Android horizontal Progress Bar example tutorial

How to use horizontal Progress Bar in android.

Horizontal progress bar is basically used to show downloading process dialogue because horizontal progress bar fill up slowly or fast as user requirement. It is apply with handler and thread So here is the complete step by step tutorial for Android horizontal Progress Bar example tutorial .

android-project-download-code-button

Android horizontal Progress Bar example tutorial.

Code for MainActivity.java file.

package com.android_examples.com.progressbarhorizontal;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends Activity {

 ProgressBar Progressbar;
 TextView ShowText;
 int progressBarValue = 0;
 Handler handler = new Handler();
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 Progressbar = (ProgressBar)findViewById(R.id.progressBar1);
 ShowText = (TextView)findViewById(R.id.textView1);
 
 new Thread(new Runnable() {
 
 @Override
 public void run() {
 // TODO Auto-generated method stub
 while(progressBarValue < 100)
 {
 progressBarValue++;
 
 handler.post(new Runnable() {
 
 @Override
 public void run() {
 
 Progressbar.setProgress(progressBarValue);
 ShowText.setText(progressBarValue +"/"+Progressbar.getMax());
 
 }
 });try {
 Thread.sleep(300);
 } catch (InterruptedException e) {
 e.printStackTrace();
 }
 }
}
}).start();
}
 
}

Code for activity_main.xml layout file.

 <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="com.android_examples.com.progressbarhorizontal.MainActivity" >

 <ProgressBar
 android:id="@+id/progressBar1"
 style="?android:attr/progressBarStyleHorizontal"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="91dp"
 android:minHeight="60dp"
 android:minWidth="220dp" />

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignBottom="@+id/progressBar1"
 android:layout_centerHorizontal="true"
 android:layout_marginBottom="42dp"
 android:text="Text Here"
 android:textAppearance="?android:attr/textAppearanceLarge"
 />

</RelativeLayout>

Screenshots:

Android horizontal Progress Bar example tutorial progress bar-2

Click Here to Download Android horizontal Progress Bar example tutorial project.

2 Comments

  1. Kindly tell me how to hide the progress bar and text when it complete to 100?

  2. Sallu Just set Progress bar and textView visibility gone using progressbar.setVisibility(View.GONE), after the public void run method end.

Leave a Reply

Your email address will not be published. Required fields are marked *