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 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: