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:
When the count down timer will be completed the progress bar will show its status as “tast Completed” as below:
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”.
23 best smartphone tips and tricks to impress your friends
Web Development Trends 2018: Your Ultimate Top 10
5 emerging programming languages with a bright future
Top DevOps Tools: 50 Reliable, Secure, and Proven Tools for All Your DevOps Needs
11 web design tools you can’t live without
38 Linux Commands and Utilities You CANT Live without
7 Resources to Sharpen your C programming skills
5 Best Resources to Get Started with Android Nougat