Tags

Progress bar adalah salah satu komponen yg bersifat grafis yang fungsinya biasanya menampilkan proses tertentu, seperti proses download file, copy file, ataupun mengindikasikan proses lainnya.

berikut adalah detailed dari layout dari gambar diatas.

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="15px">

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Loading..."/>

<ProgressBar android:id="@+id/progress"
android:paddingTop="10px"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="100" />

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/txt"/>

</LinearLayout>

ProgressbarActivity.java

package com.wilis.progressbar;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;
import android.widget.TextView;

public class ProgressbarActivity extends Activity {
	 ProgressBar bar;
	 TextView txt;
	 int total=0;
	 boolean isRunning=false;
	 // handler for the background updating
	 Handler handler=new Handler() {
	 @Override
	 public void handleMessage(Message msg) {
	 total=total+5;
	 String perc=String.valueOf(total).toString();
	 txt.setText(perc+"% completed");
	 bar.incrementProgressBy(5);
	 }
	 };
	@Override
	public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);
	bar=(ProgressBar)findViewById(R.id.progress);
	 txt=(TextView)findViewById(R.id.txt);
	}
	public void onStart() {
	 super.onStart();
	 // reset the bar to the default value of 0
	 bar.setProgress(0);
	  // create a thread for updating the progress bar
	 Thread background=new Thread(new Runnable() {
	 public void run() {
	 try {
	 for (int i=0;i<20 && isRunning;i++) {
	 // wait 1000ms between each update
	 Thread.sleep(1000);
	 handler.sendMessage(handler.obtainMessage());
	 }
	 }
	 catch (Throwable t) {
	   }     }     });
	 isRunning=true;
	  // start the background thread
	 background.start();
	 }
	 public void onStop() {
	 super.onStop();
	 isRunning=false;
	 }
	}