Android ProgressBar with Examples | How To Use Progressbar

Android ProgressBar – Introduction

Android ProgressBar –

Create activity_progress_bar File in XML

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:layout_marginTop=”20dp”
android:orientation=”vertical”
android:gravity=”center”
tools:context=”.progressBarActivity”>

<TextView
android:id=”@+id/progressTextView”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Click on Start Progress Button”
android:textSize=”20sp”
android:textStyle=”italic”
android:layout_gravity=”center_horizontal” />

<ProgressBar
android:id=”@+id/progressBar”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
style=”@style/Base.Widget.AppCompat.ProgressBar.Horizontal”
android:layout_marginTop=”15dp” />

<Button
android:id=”@+id/progressButton”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Start Progress”
android:textStyle=”bold”
android:layout_gravity=”center”
android:textAllCaps=”false”
android:backgroundTint=”@color/teal_200″
android:textColor=”@color/black”
android:layout_marginTop=”15dp”/>

</LinearLayout>

Create progressBarActivity File in Java

package com.eiheducation.demo;

import androidx.appcompat.app.AppCompatActivity;

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 progressBarActivity extends AppCompatActivity {

TextView progressTextView; // declared the TextView objects
ProgressBar progressBar; // declared the ProgressBar objects
Button progressButton; // declared the Button objects

Handler handler;
int progress=0;

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

// We need to find our elements by IDs

progressTextView = findViewById(R.id.progressTextView);
progressBar = findViewById(R.id.progressBar);
progressButton = findViewById(R.id.progressButton);

// Button Code

progressButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (progressBar.getProgress() < 100) {
progressBar.setProgress(progress);
progress++;
handler.postDelayed(this, 100);
progressTextView.setText(“Wait…”);
} else progressTextView.setText(“finished”);
}

},100);
}
});
}
}

Android Manifest File

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.eiheducation.demo”>

<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@style/Theme.EIHeducationDemo”>
<activity
android:name=”.MainActivity”
android:label=”@string/app_name”
android:theme=”@style/Theme.EIHeducationDemo.NoActionBar”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity android:name=”.relativeLayout” />
<activity android:name=”.linearLayout” />
<activity android:name=”.tableLayout” />
<activity android:name=”.frameLayout” />
<activity android:name=”.textviewLayout” />
<activity android:name=”.editTextLayout” />
<activity android:name=”.buttonActivity” />
<activity android:name=”.buttonImageActivity” />
<activity android:name=”.radioButtonActivity” />
<activity android:name=”.checkBoxActivity” />
<activity android:name=”.toggleButton” />
<activity android:name=”.switchActivity” />
<activity android:name=”.progressBarActivity”></activity>
</application>

</manifest>

Android ProgressBar – Output

Leave a Reply

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