Skip to main content

How to use ProgressBar custom Drawable 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="#e5e8e2"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif-condensed"
        android:textColor="#676a6b"
        />
    <!-- Default color/drawable progress bar -->
    <ProgressBar
        android:id="@+id/pb_default"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        style="@android:style/Widget.Holo.ProgressBar.Horizontal"
        android:layout_below="@id/tv"
        android:layout_marginBottom="15dp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Progress bar default drawable"
        android:layout_toRightOf="@id/pb_default"
        android:fontFamily="sans-serif-thin"
        android:textSize="10dp"
        android:layout_alignBottom="@id/pb_default"
        android:paddingLeft="10dp"
        />
    <!--
        android:progressDrawable
            Drawable used for the progress mode.

            Must be a reference to another resource, in the form "@[+][package:]type:name"
            or to a theme attribute in the form "?[package:][type:]name".

            This corresponds to the global attribute resource symbol progressDrawable.
    -->
    <!-- Progress bar custom drawable by XML -->
    <ProgressBar
        android:id="@+id/pb_drawable"
        android:layout_width="225dp"
        android:layout_height="wrap_content"
        style="@android:style/Widget.Holo.ProgressBar.Horizontal"
        android:layout_below="@id/pb_default"
        android:progressDrawable="@drawable/progressbar_states"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Progress bar custom drawable"
        android:layout_toRightOf="@id/pb_drawable"
        android:fontFamily="sans-serif-thin"
        android:textSize="10dp"
        android:layout_alignBottom="@id/pb_drawable"
        android:paddingLeft="10dp"
        />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start Operation"
        android:layout_below="@id/pb_drawable"
        android:layout_marginTop="25dp"
        />
</RelativeLayout>
res/drawable/progressbar_states.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape android:shape="rectangle">
            <solid android:color="#ff0020"/>
            <corners
                android:bottomRightRadius="10dp"
                android:topRightRadius="10dp"
                />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <gradient
                    android:startColor="#588f66"
                    android:centerColor="#76b787"
                    android:centerY="0.50"
                    android:endColor="#588f66"
                    android:angle="270"
                    />
                <corners
                    android:bottomRightRadius="10dp"
                    android:topRightRadius="10dp"
                    />
            </shape>
        </clip>
    </item>
</layer-list>
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_default = (ProgressBar) findViewById(R.id.pb_default);
        final ProgressBar pb_drawable = (ProgressBar) findViewById(R.id.pb_drawable);

        /*
            public void setProgressDrawable (Drawable d)
                Define the drawable used to draw the progress bar in progress mode.

                Parameters
                d : the new drawable
        */

        // Set progress bar custom drawable programmatically
        // Uncomment the below line to activate it
        //pb_drawable.setProgressDrawable(getDrawable(R.drawable.progressbar_states));

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Set the progress status zero on each button click
                progressStatus = 0;

                // 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_default.setProgress(progressStatus);
                                    pb_drawable.setProgress(progressStatus);
                                    // Show the progress on TextView
                                    tv.setText(progressStatus+"");
                                    // If task execution completed
                                    if(progressStatus == 100){
                                        // Set a message of completion
                                        tv.setText("Operation completed.");
                                    }
                                }
                            });
                        }
                    }
                }).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="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="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="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:orientation="vertical" android:layout_margin="25dp" tools:context=".MainActivity" > <TextView android:id=&…