Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Android Example: Progress Bar Tutorial (With Secondary Progress)

DZone's Guide to

Android Example: Progress Bar Tutorial (With Secondary Progress)

· Mobile Zone
Free Resource

Progress bars are used to show progress of a task. In this example, we will show you a simple application to illustrate the use of progress bar in Android application development. It mainly use the class android.widget.ProgressBar.

In this sample application, there is a secondary progress displayable on a progress bar which is useful for displaying intermediate progress, such as the buffer level during a streaming playback progress bar.

Source Code:
activity_main.xml
This is the XML file for the layout of the application.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<ProgressBar
android:id="@+id/firstBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<ProgressBar
android:id="@+id/secondBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="begin"/>
</LinearLayout> 

Activity01.java This is the main Activity class. 

package com.easyinfogeek.progress;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

public class Activity01 extends Activity {

 private ProgressBar firstBar = null;
 private ProgressBar secondBar = null;
 private Button myButton;
 private int i = 0;

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

        firstBar = (ProgressBar)findViewById(R.id.firstBar);
        secondBar = (ProgressBar)findViewById(R.id.secondBar);
        myButton = (Button)findViewById(R.id.myButton);
        myButton.setOnClickListener(new ButtonListener());

    }

    class ButtonListener implements OnClickListener{
  @Override
  public void onClick(View v) {
   if (i == 0 || i == 10) {
    //make the progress bar visible
    firstBar.setVisibility(View.VISIBLE);
    firstBar.setMax(150);
    secondBar.setVisibility(View.VISIBLE);
   }else if ( i< firstBar.getMax() ) {
    //Set first progress bar value
    firstBar.setProgress(i);
    //Set the second progress bar value
    firstBar.setSecondaryProgress(i + 10);
   }else {

    firstBar.setProgress(0);

    firstBar.setSecondaryProgress(0);

    i = 0;
    firstBar.setVisibility(View.GONE);
    secondBar.setVisibility(View.GONE);
   }
   i = i + 10;
  }
    }
}

Original URL: http://www.easyinfogeek.com/2015/03/android-example-progress-bar-tutorial.html


Topics:
mobile ,android ,tutorial

Published at DZone with permission of Tc Duncan Lam. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}