Start stop progress bar on button click in android

Set on off function on horizontal progress bar using button click.

Progress bar is easily start and stop using button click event because it is basically apply to create downloading progress bar and user can stop and start download using button click. So here is the complete step by step tutorial for Start stop progress bar on button click in android.

android-project-download-code-button

Start stop progress bar on button click in android.

Code for MainActivity.java file.

package com.android_examples.com.startprogressbaronbutton;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends Activity {

 ProgressBar Progressbar;
 TextView ShowText;
 int progressBarValue = 0;
 Handler handler = new Handler();
 Button StartProgressBar,StopProgressBar;
 boolean isStart;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 Progressbar = (ProgressBar)findViewById(R.id.progressBar1);
 ShowText = (TextView)findViewById(R.id.textView1);
 StartProgressBar = (Button)findViewById(R.id.button1);
 StopProgressBar = (Button)findViewById(R.id.button2);
 
 StartProgressBar.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 isStart = true;
 }
 });
 
 StopProgressBar.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 isStart = false;
 
 }
 });
 handler = new Handler()
 {
 public void handleMessage(android.os.Message msg) 
 {
 if(isStart)
 {
 progressBarValue++;
 }
 Progressbar.setProgress(progressBarValue);
 ShowText.setText(String.valueOf(progressBarValue/60)+":"+String.valueOf(progressBarValue%60));
 
 handler.sendEmptyMessageDelayed(0, 100);
 }
 };
 
 handler.sendEmptyMessage(0);
 }
}

Code for activity_main.xml layout file.

<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: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.android_examples.com.startprogressbaronbutton.MainActivity" >

 <ProgressBar
 android:id="@+id/progressBar1"
 style="?android:attr/progressBarStyleHorizontal"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="91dp"
 android:minHeight="60dp"
 android:minWidth="220dp" />

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignBottom="@+id/progressBar1"
 android:layout_centerHorizontal="true"
 android:layout_marginBottom="42dp"
 android:text="Text Here"
 android:textAppearance="?android:attr/textAppearanceLarge"
 />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/progressBar1"
 android:layout_centerHorizontal="true"
 android:text="Click HERE TO START PROGRESS BAR" />

 <Button
 android:id="@+id/button2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/button1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="19dp"
 android:text="CLICK HERE TO STOP PROGRESS BAR" />

</RelativeLayout>

Screenshots:

start progress bar Start stop progress bar on button click in android

Click Here To Download Start stop progress bar on button click in android project.

Leave a Reply

Your email address will not be published. Required fields are marked *