Skip to main content

How to show ProgressDialog in Android


        android:text="Start Operation"


import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.os.Handler;

public class MainActivity extends Activity {
    private int progressStatus = 0;
    private Handler handler = new Handler();

    protected void onCreate(Bundle savedInstanceState) {

        // Get the widgets reference from XML layout
        final RelativeLayout rl = (RelativeLayout) findViewById(;
        final Button btn = (Button) findViewById(;

        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                // ProgressDialog(Context context)
                // Initialize a new instance of progress dialog
                final ProgressDialog pd = new ProgressDialog(MainActivity.this);

                    setTitle(CharSequence title)
                        Set the title text for this dialog's window.
                // Set the title of progress dialog
                pd.setTitle("Title of Progress Dialog");

                // setMessage(CharSequence message)
                // Set the message of progress dialog
                pd.setMessage("Please wait until complete....");

                // setIndeterminate(boolean indeterminate)
                // Set the indeterminate mode

                // setProgressStyle(int style)
                // Set the progress style STYLE_HORIZONTAL or STYLE_SPINNER

                    setCancelable(boolean flag)
                        Sets whether this dialog is cancelable with the BACK key.
                // Set the progress dialog is cancelable or not

                // setMax(int max)
                // Set the maximum progress

                // Finally, show the progress dialog

                // Set the progress status zero on each button click
                progressStatus = 0;

                // Start the lengthy operation in a background thread
                new Thread(new Runnable() {
                    public void run() {
                        while(progressStatus < 100){
                            // Update the progress status
                            progressStatus +=1;

                            // Try to sleep the thread for 20 milliseconds
                            }catch(InterruptedException e){

                            // Update the progress bar
                   Runnable() {
                                public void run() {
                                    // Update the progress status
                                    // If task execution completed
                                    if(progressStatus == 100){
                                        // Dismiss/hide the progress dialog
                }).start(); // Start the operation

More android examples

Popular posts from this blog

How to use NumberPicker in Android

Table of contentsNumberPicker ExamplesetOnValueChangedListeneronValueChange()setWrapSelectorWheel()setMinValue()setMinValue()setMaxValue()NumberPicker with String ArraysetDisplayedValues()String Array1. NumberPicker Example activity_main.xml <RelativeLayout xmlns:android="" xmlns:tools="" android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" tools:context=".MainActivity" android:background="#ffffff" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25dp" android:text="Select a number..." /> <NumberPicker android:id="@+id/np" …

TextView new line (multiline) in android

TextView new line (multiline) TextView display text on android app. by default, TextView show text on a single line, and if it is long then TextView take more lines to display its text. android developers can generate a new line on TextView both programmatically and syntactically. android developers can make a multiline TextView without splitting text to multiline by android:minLines attribute.

the following android example code demonstrate us how can we syntactically create a new line on TextView widget by xml layout file and string resource file.

the simplest way to create a new line on TextView is android:text attribute. android:text attribute allow us to display text on android app. we can add a simple '\n' to TextView text where we want to start a new line. in this way we can create a multiline TextView widget in android app. we can assign android:text attribute value by this way android:text="Line1 \n Line2 \n Line3" for a TextView widget. this value will…

How to change TextView font size in android

TextView font size TextView widget display text on android application. we can set or change TextView font size statically by declarative syntax in xml layout file or programmatically at run time in java file. even we can use an xml file source to define font size.

the following example code demonstrate us how can we define TextView font size in xml layout file and how can we uses dimens.xml to reference font size. in this example we did not changes any coding in java file, so here we only include the layout xml file and dimens.xml file. activity_main.xml <LinearLayout xmlns:android="" xmlns:tools="" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_margin="25dp" tools:context=".MainActivity" > <TextView android:id=&…