Android progress bar

In this Android tip, I am going to show you how to use the ProgressBar widget to display a progress bar to the user. A progress bar tells the user how far an operation progressed. To define a progress bar in a layout xml file, you will use the <ProgressBar> tag. There are two styles of the progress bar: spinning wheel, and horizontal bar. By default, the <ProgressBar> tag shows a spinning wheel. The spinning wheel style is used when the length of an operation is unknown. If you want the second style you can use the style attribute as shown below.
style="?android:attr/progressBarStyleHorizontal"
The application that uses the progress bar can specify its starting and max values. While the operation is progressing, you can use a worker thread to update the current value of the progress bar.
Now to have an example application on using ProgessBar, you create a new Android project. Then modify the activity_main.xml file to add a ProgessBar as shown below.


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"  
   tools:context=".MainActivity"
   android:orientation="vertical"
   >
   <ProgressBar
     android:id="@+id/progress_bar"
     style="?android:attr/progressBarStyleHorizontal"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
      
  
    />
  
</LinearLayout>


In the MainActivity class, you will create a reference to progress bar, set its starting and max values, and setup a worker thread to start and update the progress bar as shown below.

package com.example.androidexample;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
  Handler handler;
  ProgressBar pb;
  int maxValue=100;
  int curValue=0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); pb=(ProgressBar)findViewById(R.id.progress_bar);
//set maximum value to progressbar
pb.setMax(maxValue);  
//set the start value to progressbar
pb.setProgress(curValue);
//create an Instance of Handler
handler=new Handler();
//start Runnable
handler.post(UpdateProgess);
}
Runnable UpdateProgess=new Runnable(){
  public void run(){
  curValue++;
  pb.setProgress(curValue);
  //delay 1/2s before next call
  handler.postDelayed(this, 500);  
  }
};
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}



Android-progress-bar

Posted by: Dara | post date: 09-15-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.