Android ProgressBar Example

activity_main.xml
 <RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/rl"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:padding="10dp"     tools:context=".MainActivity"     android:background="#f0f5ee"     >     <ProgressBar         android:id="@+id/pb"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:visibility="gone"         style="@android:style/Widget.Holo.ProgressBar"         />     <TextView         android:id="@+id/tv"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:visibility="gone"         android:layout_toRightOf="@id/pb"         android:textSize="35dp"         android:fontFamily="sans-serif-condensed"         android:textColor="#7c8c79"         android:layout_alignBaseline="@id/pb"         />     <Button         android:id="@+id/btn"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Start Operation"         android:layout_below="@id/tv"         android:layout_marginTop="25dp"         /> </RelativeLayout> 
MainActivity.java
 package com.cfsuman.me.androidcodesnippets;  import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.ProgressBar; import android.os.Handler;   public class MainActivity extends Activity {     private int progressStatus = 0;     private Handler handler = new Handler();      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          // Get the widgets reference from XML layout         selesai RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);         selesai Button btn = (Button) findViewById(R.id.btn);         selesai TextView tv = (TextView) findViewById(R.id.tv);         selesai ProgressBar pb = (ProgressBar) findViewById(R.id.pb);          btn.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View view) {                 // Set the progress status zero on each button click                 progressStatus = 0;                 // Visible the progress kafetaria and text view                 pb.setVisibility(View.VISIBLE);                 tv.setVisibility(View.VISIBLE);                  // Start the lengthy operation in a background thread                 new Thread(new Runnable() {                     @Override                     public void run() {                         while(progressStatus < 100){                             // Update the progress status                             progressStatus +=1;                              // Try to sleep the thread for 20 milliseconds                             try{                                 Thread.sleep(20);                             }catch(InterruptedException e){                                 e.printStackTrace();                             }                              // Update the progress kafetaria                             handler.post(new Runnable() {                                 @Override                                 public void run() {                                     pb.setProgress(progressStatus);                                     // Show the progress on TextView                                     tv.setText(progressStatus+"");                                     // If task execution completed                                     if(progressStatus == 100){                                         // Hide the progress kafetaria from layout after finishing task                                         pb.setVisibility(View.GONE);                                         // Set a message of completion                                         tv.setText("Operation completed...");                                     }                                 }                             });                         }                     }                 }).start(); // Start the operation             }         });     } } 




More android examples

Komentar