Home / Android Studio Codes / Show Hide progress bar on Button click in Android

Show Hide progress bar on Button click in Android

Enable disable progress bar visibility on android.

Progress bar mainly shows when application developer wants to download something in background and after done downloading it will hide or display the progress bar automatically. You can also hide progress bar on button click event using progress bar visibility settings. So here is the complete step by step tutorial for Show Hide progress bar on Button click in Android.

Show Hide progress bar on Button click in Android.

Code for MainActivity.java file.

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

public class MainActivity extends Activity {
 
 ProgressBar Pbar;
 Button ShowPorgressBar,HideProgressBar;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 Pbar = (ProgressBar)findViewById(R.id.progressBar1);
 ShowPorgressBar = (Button)findViewById(R.id.button2);
 HideProgressBar = (Button)findViewById(R.id.button1);
 
 ShowPorgressBar.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 Pbar.setVisibility(View.VISIBLE);
 
 }
 });
 
 HideProgressBar.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 
 Pbar.setVisibility(View.GONE);
 
 }
 });
 
 }
}

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.showhideprogressbar.MainActivity" >

 <ProgressBar
 android:id="@+id/progressBar1"
 style="?android:attr/progressBarStyleLarge"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="28dp" />

 <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:layout_marginTop="51dp"
 android:text="Click Here to Hide 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:text="Click Here To Show Progress Bar" />

</RelativeLayout>

About vikas agrahari

Check Also

Dynamically add values in string array list in android Java

Insert elements in array using arrayName.add() function on app program running time. Dynamically adding values in …