Updating Your Progressbar With A Count Down Timer

A Progress bar in android represents the progress of any task user has assigned in graphical view. In short, it shows a bar which represents the completion of a task. It is very common component in any user interface.

 

Check my blog on progress bar I have posted long ago.

Today I will show you how you can link progress bar and countdown timer together in programming. I mean We will learn how to update your progress bar with countdown timer.

Let’s get started…. you just need to follow the steps given below and you will be all set.

Step1: Create a new android project in your android environment.

Step2: Write following into your main layout:

 

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<ProgressBar
android:id="@+id/progressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="100" />
<Button
android:id="@+id/start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start" />
<TextView
android:id="@+id/counter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textStyle="bold"
android:textSize="50sp"
android:gravity="center" />

</LinearLayout>

 

Step3: Write following into you main activity file:

 

package com.example.updatingprogressbar;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

Button buttonStart;
ProgressBar progressBar;
TextView textCounter;

MyCountDownTimer myCountDownTimer;

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

buttonStart = (Button)findViewById(R.id.start);
progressBar = (ProgressBar)findViewById(R.id.progressbar);
textCounter = (TextView)findViewById(R.id.counter);

buttonStart.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
progressBar.setProgress(100);
myCountDownTimer = new MyCountDownTimer(10000, 500);
myCountDownTimer.start();
}});

}

public class MyCountDownTimer extends CountDownTimer {

public MyCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}

@Override
public void onTick(long millisUntilFinished) {
textCounter.setText(String.valueOf(millisUntilFinished));
int progress = (int) (millisUntilFinished/100);
progressBar.setProgress(progress);
}

@Override
public void onFinish() {
textCounter.setText("Task completed");
progressBar.setProgress(0);
}

}
}

 

Step4: Run to see the output below:

progress1

When the count down timer will be completed the progress bar will show its status as “tast Completed” as below:

progress2

Hope you like this post. Read www.edumobile.org/android to enhance your android programming.

Check out my previous blog on “Scroll to a Selected position in list view”.

Leave a Comment: