As in our last tutorial we discussed about CardView in android , Now in this tutorial I am going to discuss about How to implement progress bar in android app ?

A dialog showing a progress indicator and an optional text message or view. Only a text message or a view can be used at the same time. ProgressDialog is an extends class of AlertDialog or It is an extension of AlertDialog . ProgressDialog is used to show the progress of a task that takes some time to complete . ProgressDialog creates a popup that display the progress of the task .

Now before you start implementing it, I want to tell you that ProgressDialog class has been deprecated in API level 26 . So, instead of using this class, you should use a progress indicator like ProgressBar.

1. Create a New Project :

Lets we start with a new project in Android Studio from File New Project ⇒
select Empty Activity and go through it.

2. Create a button to your main layout file :

Here we create a button to your main layout file i.e. activity_main.xml of your Main_Activity.java file or just implement the following xml code.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="12dp"
        android:text="Open Progress Dialog"
        android:layout_centerInParent="true"
        android:background="@color/colorPrimary"
        android:textColor="#fff"/>

</RelativeLayout>

3. Now open your MainActivity.java and add following code :

Now we create a ProgressBar or progress dialog view in android when we click on button using setOnClickListener( )  .

MainActivity.java :

package net.technxt.testapp;

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


    Button btn;
    ProgressDialog progressDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        btn = (Button)findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openProgressDialog();
            }
        });

    }

    private void openProgressDialog() {

        progressDialog= new ProgressDialog(MainActivity.this);
        progressDialog.setMessage("Please wait...");
        progressDialog.setTitle("Progress Dialog");
        progressDialog.setIcon(R.drawable.user);
        //To show the dialog
        progressDialog.show();
        
        //To dismiss the dialog
//        progressDialog.dismiss();

    }
}

Now just run the project/app to test progressbar or progress dialog in android app through android device or any Emulator . 

%d bloggers like this: