How to make progressbar (loading animation) in my project?

I make project like this : Fetch Messages from Inbox

Part of code :

while  (cursor.moveToNext()) 
{
    String address = cursor.getString(1);
    String body = cursor.getString(3);

    // Loading too long
    System.out.println("======> Mobile number => "+address);
    System.out.println("=====> SMS Text => "+body);
    sms.add("Address=> "+address+"\n SMS => "+body);   
}
return sms;

But, there are too many message and need long time to show. While loding i want to show the animation (progressbar)

Thanks you...

Answers


Usually you would use AsyncTask for such a thing on Android..

Intro: By default all code you write not using threads,services,etc.. will execute in UI thread.. That means that if you do some expensive work there, user interface will be blocked (not responsive). Good practise is to move such a expensive task to separate thread, otherwise you app will show ANR dialog and be closed.

How: One of the best approach for your case is use of AsyncTask.. Basically in onPreExecute() you show progress dialog or "long task in progress" animation. In doInBackground(Params... params) you put code of expensive operation. In case u need to update progress bar you do it from onProgressUpdate(Progress... values). When expensive task finishes it will provide results to onPostExecute(Result result) method. From this one you can update UI again and close previously displayed progress dialog.

Hope it helped. Cheers.. ;)


Use Asyntask, put that code in doInBackground , start processbar in onPreExecute() , dimiss in onPostExecute()


Implement Runnable in your project. Run this code

while  (cursor.moveToNext()) 
{
String address = cursor.getString(1);
String body = cursor.getString(3);

// Loading too long
System.out.println("======> Mobile number => "+address);
System.out.println("=====> SMS Text => "+body);
sms.add("Address=> "+address+"\n SMS => "+body);   
}
//return sms;// Make a global variable which can hold values here...
handler.sendEmptyMessage(0);

in

run()

method. Now start the thread like this:

Thread fetchSmsThread = new Thread(this);
progressDialog = ProgressDialog.show(context,title,message);
fetchSmsThread.start();

Handler Code:

 Handler handler = new Handler()
 {

  //////Catch Message Here.. and Dismiss dialog...
  ///Note You have to declare progress dialog as global so that you can access it here...

 }

For further help about runnable follow this:

How to run a Runnable thread in Android?

If you dont want to use Runnable then use Android AsynClass, follow:

AsyncTask Android example


Add a Progressbar in main.xml with an id (yourProgressBarId in the example below):

@Override  
public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.main);  

    final ListView lViewSMS = (ListView) findViewById(R.id.listViewSMS);  

    final ProgressBar progressBar = (ProgressBar)findViewById(R.id.yourProgressBarId);
    progressBar.setVisibility(View.VISIBLE);
    new Thread(){
        public void run() {

                ArrayList sms =fetchInbox();
                if(sms!=null) 
                lViewSMS.post(new Runnable() {

                    @Override
                    public void run() {
                         ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, sms);  
                         lViewSMS.setAdapter(adapter);  
                         progressBar.setVisibilty(View.GONE);
                }
          });
    }   
}.start();

}  

Need Your Help