Monday, August 31, 2015

How to show an indeterminate ProgressBar in Android

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#e4e8de"
    >
    <!--
        android:indeterminate
            Allows to enable the indeterminate mode.

        android:indeterminateBehavior
            Defines how the indeterminate mode should behave when the progress reaches max.

        android:indeterminateDrawable
            Drawable used for the indeterminate mode.

        android:indeterminateDuration
            Duration of the indeterminate animation.

        android:indeterminateOnly
            Restricts to ONLY indeterminate mode (state-keeping progress mode will not work).

        android:indeterminateTint
            Tint to apply to the indeterminate progress indicator.

        android:indeterminateTintMode
            Blending mode used to apply the indeterminate progress indicator tint.
    -->
    <ProgressBar
        android:id="@+id/pb"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:visibility="gone"
        style="@android:style/Widget.Holo.ProgressBar.Horizontal"
        android:indeterminate="true"
        />
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:layout_toRightOf="@id/pb"
        android:textSize="15dp"
        android:fontFamily="sans-serif-condensed"
        android:textColor="#ff2f33"
        android:layout_marginLeft="10dp"
        />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start Operation"
        android:layout_below="@id/tv"
        android:layout_marginTop="25dp"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ProgressBar;
import android.os.Handler;


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

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

        // Get the widgets reference from XML layout
        final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
        final Button btn = (Button) findViewById(R.id.btn);
        final TextView tv = (TextView) findViewById(R.id.tv);
        final ProgressBar pb = (ProgressBar) findViewById(R.id.pb);

        /*
            setIndeterminate(boolean indeterminate)
                Change the indeterminate mode for this progress bar.
        */
        // Programmatically setting the progress bar indeterminate mode
        //pb.setIndeterminate(true);

        // Other methods related to progress bar indeterminate mode
        /*
            setIndeterminateDrawable(Drawable d)
                Define the drawable used to draw the progress bar in indeterminate mode.

            setIndeterminateDrawableTiled(Drawable d)
                Define the tileable drawable used to draw the progress bar in indeterminate mode.

            setIndeterminateTintList(ColorStateList tint)
                Applies a tint to the indeterminate drawable.

            setIndeterminateTintMode(PorterDuff.Mode tintMode)
                Specifies the blending mode used to apply the tint specified by
                setIndeterminateTintList(ColorStateList) to the indeterminate drawable.
        */

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Set the progress status zero on each button click
                progressStatus = 0;
                // Visible the progress bar and text view
                pb.setVisibility(View.VISIBLE);
                tv.setVisibility(View.VISIBLE);

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

                            // Try to sleep the thread for 20 milliseconds
                            try{
                                Thread.sleep(20);
                            }catch(InterruptedException e){
                                e.printStackTrace();
                            }

                            // Update the progress bar
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    pb.setProgress(progressStatus);
                                    // Show the progress on TextView
                                    tv.setText(progressStatus+"");
                                    // If task execution completed
                                    if(progressStatus == 100){
                                        // Hide the progress bar from layout after finishing task
                                        pb.setVisibility(View.GONE);
                                        // Set a message of completion
                                        tv.setText("Operation completed...");
                                    }
                                }
                            });
                        }
                    }
                }).start(); // Start the operation
            }
        });
    }
}




More android examples