Android ProgressBar Tutorial

A progress bar is a graphical control element used to show the process of the operation, such as downloading, file transfer, or installation.

Step 1.

Create new application with any name from file menu option.

File → New Project → Application name → select API → Add Blank Activity → Activity Name → finish.

Step 2.

In activity_main.xml layout file design your application.

Add following Controls:-

progress bar tutorial

Step 3.

Import Package and write following coding in MainActivity.java class file.

	
    import android.widget.ProgressBar;
    import android.widget.Button;
    import android.widget.Toast;

	public class MainActivity extends ActionBarActivity 
	{
	Button b1;
	ProgressBar pr;
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		b1=(Button)findViewById(R.id.bt);
		pr=(ProgressBar)findViewById(R.id.progressBar);
		b1.setOnClickListener(new View.OnClickListener() 
	{
	@Override
		public void onClick(View v)
	{
		pr.setProgress(0);
		new Thread(new Hello()).start();
	}
		});
	}
	class Hello implements Runnable
	{
		public void run()
	{
	for(int i=0;i<100;i=i+2)
	{
		try
		{
		Thread.sleep(1000);
		}
		catch (InterruptedException e)
		{
		e.printStackTrace();
		}
		pr.setProgress(i);
			if(i==100)
			{
			Toast.makeText(MainActivity.this,"Progress Completed",Toast.LENGTH_LONG).show();
			}
    	}
   }
  }
    

Step 4. Final Output

Now run your application.

progress bar tutorial

Post Your Comment