How to make an dialog box with horizontal progress bar inside in with TextView percentage text.
In this tutorial we are calling two different-different methods on button click events CreateProgressDialog() and ShowProgressDialog(). In our first method we are dynamcially creating progress dialog with horizontal progress bar and on second method we are simply filling the progress bar with the use of thread and handler. So here is the complete step by step tutorial for Create Show Progress Dialog with percentage text inside in android.
How to Create Show Progress Dialog with percentage text inside in android.
Code for MainActivity.java file.
package com.progressdialogwithpercentage_android_examples.com; import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { Handler handler = new Handler(); int status = 0; Button button; ProgressDialog progressdialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { CreateProgressDialog(); ShowProgressDialog(); } }); } public void CreateProgressDialog() { progressdialog = new ProgressDialog(MainActivity.this); progressdialog.setIndeterminate(false); progressdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressdialog.setCancelable(true); progressdialog.setMax(100); progressdialog.show(); } public void ShowProgressDialog() { status = 0; new Thread(new Runnable() { @Override public void run() { while(status < 100){ status +=1; try{ Thread.sleep(200); }catch(InterruptedException e){ e.printStackTrace(); } handler.post(new Runnable() { @Override public void run() { progressdialog.setProgress(status); if(status == 100){ progressdialog.dismiss(); } } }); } } }).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.progressdialogwithpercentage_android_examples.com.MainActivity" android:id="@+id/relativelayout" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="Show Progress Dialog with percentage text inside in android" /> </RelativeLayout>
Screenshots: