Google+

Wednesday, June 29, 2011

How to Create ProgressBar in Android using XML

How to use progress bar using Layout.xml file
follow the steps....
Step : 1 -
Create XML with <ProgressBar .....> as given below


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="My Progress Bar Demo" />

    <ProgressBar android:id="@+id/progressbar"
        android:layout_width="wrap_content" android:layout_height="wrap_content"  android:layout_gravity="center" style="?android:attr/progressBarStyleLarge"/>

</LinearLayout>



Note: You can change the ProgressBar Style to 
  • android:progressBarStyle
  • android:progressBarStyleHorizontal
  • android:progressBarStyleLarge
  • android:progressBarStyleSmall
See the screen Show Below


Step : 2 -

Create an Activity to show progress bar on Screen

public class MyProgressBar extends Activity 
{    
    private static int progress = 0;
    private ProgressBar progressBar;
    private int progressStatus = 0;
    private Handler handler = new Handler();
 
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.basicviews);
 
        progressBar = (ProgressBar) findViewById(R.id.progressbar);
 
        //---do some work in background thread---
        new Thread(new Runnable() 
        {
            public void run() 
            {
                //---do some work here---
                while (progressStatus < 10) 
                {
                    progressStatus = taskToDo();
                }
 
                //---hides the progress bar---
                handler.post(new Runnable() 
                {
                    public void run() 
                    {
                        progressBar.setVisibility(View.GONE);
                    }
                });
            }    
 
            //---do some long lasting work here---
            private int taskToDo() 
            {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) 
                {
                    e.printStackTrace();
                }
                return ++progress;
            }
 
        }).start();  
    }
}












No comments:

Google+