Progress Bar On Android
By SoEnLion, 10th Feb 2010 | Follow this author
| RSS Feed | Short URL http://nut.bz/4ws2d1y7/
Posted in WikinutGuidesTechnologyMobile Phones
To present the progress of loading in most of the applications, we often use progress bars.
Introduction
Most of the applications (mobile, desktop...) need to charge some resources for a further use, from the local system or a remote one (like for web apps). A standard way to present the progress of this loading is the progress bar. The progress bar may have different forms. The most used ones are the circular one (for an undermined duration or amount of resources) or linear one for the other case.
In this tutorial, we’ll try to use the XML layout file, to present those two types of presenting the progress of loading.
The HowTo
To implement a ProgressBar, a Runnable Thread is used transmitt a message to a Handle to move the progress forward in the ProgressBar.
To do it we'll have to modify the XML layout file main.xml (under res/layout) to add those two ProgressBar.
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressbar_default"
/>
<ProgressBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:id="@+id/progressbar_Horizontal"
android:max="100"
/>
</LinearLayout>Once finished with the layout file, we modify the main java file (MainActivity here) like this:
package com.wikinut.android;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
ProgressBar myProgressBar;
int myProgress = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myProgressBar=(ProgressBar)findViewById(R.id.progressbar_Horizontal);
new Thread(myThread).start();
}
private Runnable myThread = new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
while (myProgress<100){
try{
myHandle.sendMessage(myHandle.obtainMessage());
Thread.sleep(1000);
}
catch(Throwable t){
}
}
}
Handler myHandle = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
myProgress++;
myProgressBar.setProgress(myProgress);
}
};
};
}Run that as usual and you should have something similar to this:
http://img.wikinut.com/img/pu4arj42koyyxtqx/jpeg/0/preview.jpeg

Comments
27th Sep 2010 (#)
A few pictures would have been helpful to get the idea.
Reply to this comment
24th Nov 2010 (#)
Thank a lot of!
Reply to this comment
1st Feb 2011 (#)
Hello All,
Can u guys please help me how to set vertical progress bar style in android application. i have shown Horizontal progress bar with the style style="?android:attr/progressBarStyleHorizontal" but i want to display progress bar in vertical manner to show wight of information in the application
please help me guys in ASAP...
thank you in advance..
Reply to this comment
17th Apr 2011 (#)
hello
can u help me
i wrote all the code.but when i run exception will Occur..May be there is a problem of thread?can u help me
Reply to this comment
13th Jun 2011 (#)
how to get data from sqlite to android application
Reply to this comment
21st Sep 2011 (#)
Very clean example, first one with a thread and progressbar that I managed to find that doesn't crash!
Reply to this comment
6th Jan 2012 (#)
The ads you show are... inappropriate, to say the least. The one I currently see has the Swedish words for f*** and bi*** several times, and urges me to join a group so I can "f*** 2 bi***es tonight" (all spelled out very explicitly). In addition there is a very revealing picture of a young lady.
Reply to this comment