Aug 16, 2014

CountDownTimer and ProgressBar

Example of using CountDownTimer to update ProgressBar.


package com.example.androidcountdownprogressbar;

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("Finished");
   progressBar.setProgress(0);
  }
     
    }

}

<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"
    tools:context="com.example.androidcountdownprogressbar.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com" />

    <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>

3 comments:

Infolinks In Text Ads