Skip to main content

android - How to show secondary ProgressBar

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:padding="16dp"
    tools:context=".MainActivity"
    android:background="#ffffff"
    >
    <ProgressBar
        android:id="@+id/pb"
        android:layout_width="200dp"
        android:layout_height="200dp"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        android:layout_centerInParent="true"
        android:progressDrawable="@drawable/progressbar_states"
        />
    <TextView
        android:id="@+id/tv_primary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:layout_centerInParent="true"
        />
    <TextView
        android:id="@+id/tv_secondary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#3981ff"
        />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Run"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        />
</RelativeLayout>
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="oval">
            <stroke
                android:width="8dp"
                android:color="#020202"
                />
            <solid android:color="#ffffff"/>
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip android:clipOrientation="vertical" android:gravity="bottom">
            <shape
                android:shape="oval">
                <stroke
                    android:width="8dp"
                    android:color="#5eba7c"
                    />
                <solid android:color="#cfedd7"/>
            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip android:clipOrientation="vertical" android:gravity="bottom">
            <shape
                android:shape="oval">
                <stroke
                    android:width="8dp"
                    android:color="#ff0001"
                    />
                <solid android:color="#f177e9"/>
            </shape>
        </clip>
    </item>
</layer-list>
MainActivity.java

package com.cfsuman.me.androidcode;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import android.os.Handler;


public class MainActivity extends AppCompatActivity {
    private Context mContext;

    private TextView mTVPrimary;
    private TextView mTVSecondary;
    private Button mButton;
    private ProgressBar mProgressBar;

    private int mProgressStatus;
    private int mSecondaryProgressStatus;

    private Handler mHandler = new Handler();

    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Request window feature action bar
        requestWindowFeature(Window.FEATURE_ACTION_BAR);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();

        // Get the widgets reference from XML layout
        mTVPrimary = (TextView) findViewById(R.id.tv_primary);
        mTVSecondary = (TextView) findViewById(R.id.tv_secondary);
        mButton = (Button) findViewById(R.id.btn);
        mProgressBar = (ProgressBar) findViewById(R.id.pb);

        // Set a click listener for button widget
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Set the initial progress status
                mProgressStatus = 0;
                // Set the initial secondary progress status
                mSecondaryProgressStatus = 0;

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

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

                            // Update secondary progress
                            updateSecondaryProgress();
                            // Set the secondary progress status to zero after a task complete
                            mSecondaryProgressStatus = 0;

                            // Update the progress bar
                            mHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    // Set the progress for progress bar
                                    mProgressBar.setProgress(mProgressStatus);
                                    mTVPrimary.setText("Complete " + mProgressStatus + "% of 100");

                                    // If primary task complete
                                    if(mProgressStatus==100){
                                        // Show message for task completion
                                        mTVPrimary.setText("Complete.");
                                    }
                                }
                            });
                        }
                    }
                }).start(); // Start the operation in a thread
            }
        });
    }

    // Custom method to update a secondary progress
    protected void updateSecondaryProgress(){
        // Start another lengthy operation in a background thread
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(mSecondaryProgressStatus<100){
                    mSecondaryProgressStatus +=1;

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

                    // Update the secondary progress bar
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            // Set the secondary progress bar progress
                            mProgressBar.setSecondaryProgress(mSecondaryProgressStatus);
                            mTVSecondary.setText("Single task progress " + mSecondaryProgressStatus + "% of 100");

                            // If the secondary task complete
                            if(mSecondaryProgressStatus == 100){
                                // Show a confirmation message for task completion
                                mTVSecondary.setText("Single task complete.");
                            }
                        }
                    });
                }
            }
        }).start(); // Start the operation in another thread
    }
}

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=&…