Home Android AsyncTask – Update ProgressBar

Android AsyncTask – Update ProgressBar

Android AsyncTask – Update ProgressBar


Language :
Type :
Owner :
License :
Price :


Android AsyncTask ProgressBar example

This is android asynctask tutorial.We simply update a progressbar while simulating a background task.

Cheers.

Section 1 : MainActivity Class

 

package com.tutorials.asynctaskprogressbar;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MainActivity extends Activity {

  ProgressBar pb;
  Button startBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pb=(ProgressBar) findViewById(R.id.progressBar1);
        startBtn=(Button) findViewById(R.id.startBtn);

        //ONCLICK
        startBtn.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View arg0) {
        // TODO Auto-generated method stub
        new Downloader().execute();
      }
    });

    }

    class Downloader extends AsyncTask<Void, Integer, Integer>
    {

    	@Override
    	protected void onPreExecute() {
    		// TODO Auto-generated method stub
    		super.onPreExecute();

    		//SET PB PROIPERTIES
    		pb.setMax(100);

    	}
    	@Override
    	protected void onProgressUpdate(Integer... values) {
    		// TODO Auto-generated method stub
    		super.onProgressUpdate(values);

    		//UPDATE PROGRESSBAR
    		pb.setProgress(values[0]);

    	}

    @Override
    protected Integer doInBackground(Void... arg0) {
      // TODO Auto-generated method stub

      //DO HEAVY JOB
      for(int i=0;i<100;i++)
      {
        publishProgress(i);

        try
        {
          Thread.sleep(100);
        }catch(InterruptedException ie)
        {
          ie.printStackTrace();
        }
      }

      return null;
    }

    @Override
    protected void onPostExecute(Integer result) {
      // TODO Auto-generated method stub
      super.onPostExecute(result);

      Toast.makeText(getApplicationContext(), "Download Finished !!", Toast.LENGTH_LONG).show();
    }

    }


}

 

 

Section 2 : Layout Class

 

<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
                android:layout_marginTop="198dp" />

    <Button
        android:id="@+id/startBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/progressBar1"
        android:background="#009968"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="69dp"
        android:text="Start" />

</RelativeLayout>

 

 

Section 3 : Result

Android AsyncTask With ProgressBar Update

Android AsyncTask With ProgressBar Update