Progress Bar Horizantal Click and Round Click Java Android

5Ssoftwaresafety
5S JAVA AND KOTLIN LEARNING
4 min readOct 9, 2020

In this tutorial we will create a basic android Porgress Bar with two example.

ProgressBar Tutorial With Example In Android Studio. A progressBar is a view that represents progress of an operation.

Progress Bar Horizantal Click

To add a progress bar to a layout (xml) file, you can use the <ProgressBar> element.

By default, a progress bar is a spinning wheel (an indeterminate indicator).

To change to a horizontal progress bar, apply the progress bar’s horizontal style.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Create Empty Project

How to implement on Android —public void progressBarHorizontalClick(View v) for setProgressValue in android programmatically

Step 2 — “Add the following code to res/layout/activity_main.xml.“

progress button xml code

I worked in simple relative layout method for only give basic example.

  • In the below code, we have declared <ProgressBar as “@+id/progressBar” and ”@+id/progressBarHorizontal”.

RELATIVE LAYOUT FILE CODE IN XML:

<?xml version="1.0" encoding="utf-8"?>

<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"
tools:context="
com.yourname.yourpackagename.MainActivity"
android:padding="16dp"
android:id="@+id/relativeLayout"
android:orientation="vertical"
>


<ProgressBar
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/progressBar"
android:text="Click Me"
android:layout_marginTop="10dp"
android:onClick="progressBarClick"
/>

<ProgressBar
android:id="@+id/progressBarHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/button1"
android:layout_marginTop="50dp"
android:max="100"
android:progress="0"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
/>

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/progressBarHorizontal"
android:text="Click Me"
android:layout_marginTop="10dp"
android:onClick="progressBarHorizontalClick"
/>

<TextView
android:id="@+id/textViewJavaLearn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="550dp"
android:gravity="center"
android:text="5S JAVA AND KOTLIN LEARNING"
/>



</RelativeLayout>

Step 3: MainActivity.java” java file − Add the following code to src/MainActivity.java

Tip illustrates a method of implementing a progressbar in your application. The example demonstrates the ProgressBar component.

progressBarHorizontal and progressBar round

JAVA CODE IN MAIN ACTIVITY:

package com.yourname.yourpackagename;import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

ProgressBar progressBar, progressBarHorizontal;
Button button1, button2;
int progress = 0;

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

//init the progressbar
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBarHorizontal = (ProgressBar) findViewById(R.id.progressBarHorizontal);

button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);

//set visibility gone so it doesn't show on start
progressBar.setVisibility(View.GONE);
}

public void progressBarClick(View v){
//progressbar visible when button clicked
progressBar.setVisibility(View.VISIBLE);

}

public void progressBarHorizontalClick(View v){
//method to set value of progress and pass starting value = 0
setProgressValue(progress);
}

private void setProgressValue(final int progress) {
progressBarHorizontal.setProgress(progress);
//thread is used to give delay and set value accordingly
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try{
Thread.sleep(1000);
} catch(InterruptedException e){
e.printStackTrace();
}
setProgressValue(progress + 10);
}
});
thread.start();

}

}

Review Application

In this tutorial we are simply adding How to Make a Progress Bar in Java

Android ProgressBar Tutorial With Example

Step 4: Results and How to Work SEEK BAR Codes!

The following examples show how to use ProgressBar. These examples are extracted from open source projects.

When we run the application, we will get output as shown above.

That’s end of tutorial on Android ProgressBar Widget.

How to use Widget.AppCompat.ProgressBar.Horizontal in Android !

What is Progress Bar

Progress Bar JAVA

We can display the android progress bar dialog box to display the status of work being done e.g. downloading file, analyzing status of work etc.

In Android, ProgressBar is used to display the status of work being done like analyzing status of work.

If you have any question write comments I will reply.

Thanks for reading my blog.

Have a nice day.

If you need Google AdMob Alternatives you can check out Start Ad Platfrom.

https://portal.start.io/#/signup?referredby=69e578e9-ec0c-24b3-a74e-f58373fac2ef&preferredsite=pub&source=directURL

Regards

5S JAVA AND KOTLIN LEARNING

--

--