Android WebView is used to display web pages in our android application. We can use loadUrl method to load URL in WebView. In this example, we have used WebView with Horizontal ProgressBar. Android WebView Horizontal Progress Bar. Getting Started. At first, we will create android project. Then, we will create and use android horizontal progressBar programmatically in kotlin file. I copied this from one of my apps, so there's prob a few extra attributes, but should give you the idea. This is from the layout that has the progress bar. ProgressBar Tutorial With Example In Android Studio. In Android, ProgressBar is used to display the status of work being done like analyzing status of work or downloading a file etc. In Android, by default a progress bar will be displayed as a spinning wheel but If we want it to be displayed as a horizontal bar then we need to use style attribute as horizontal.
- Here, the android progress bar dialog box to display the status of work being done e.g. downloading file, analyzing status of work etc.
- The ProgressDialog class provides methods to work on progress bar like setProgress(), setMessage(), setProgressStyle(), setMax(), show() etc.
- The progress range of Progress Dialog is 0 to 10000.
- Progress bars are used to show progress of a task.
- For example, when you are uploading or downloading something from the internet, it is better to show the progress of download/upload to the user.
- In android, there is a class called ProgressDialog that allows you to create progress bar.
- In order to do this, you need to instantiate an object of this class. Its syntax is.
A Progress Bar is used to measure the progress of an ongoing task. For an instance, you can see the upload or download of an element to the or from the Internet, respectively. That’s where a progress bar comes into existence.
Also Read: Android RadioGroup Control
Android Horizontal Progress Bar Thickness Chart
Various Methods Provided by the ProgressDialog Class
The creation of a progress bar in Android is only possible with the help of the ProgressDialog class. For that, instantiate an object of the ProgressDialog class and then you are good to proceed ahead. In order to do this, follow the given syntax:
The creation of a progress bar in Android is only possible with the help of the ProgressDialog class. For that, instantiate an object of the ProgressDialog class and then you are good to proceed ahead. In order to do this, follow the given syntax:
ProgressDialog progress = new ProgressDialog(this);
Now, you can easily set the required properties for your dialog. It may range from setting a message to a specific style.
progress.setMessage(“Downloading Music ? “);
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress.setIndeterminate(true);
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress.setIndeterminate(true);
[post_middile_section_ad]
There are a few important and most commonly used methods offered by the ProgressDialog class. We have listed them out in the table provided below.
S. No. | Method | Description |
1 | getMax() | * This method retrieves or returns the max value of the Progress Dialog. |
2 | setMax(int max) | * This method sets the max value for the Progress Dialog. |
3 | setProgress(int value) | * When you are required to update the Progress Dialog with some particular values then this method is taken into consideration. |
4 | incrementProgressBy(int diff) | * This method is used to increment the progress bar by the diff value passed to it. |
5 | setIndeterminate(boolean indeterminate) | * The progress indicator can either be set as determinate or indeterminate by using this method. * It accepts either true or false. |
6 | show(Context context, CharSequence title, CharSequence message) | * This is a static method. * This method is used to display the Progress Dialog. |
The following example will help you to include a progress bar in your application. The display of the progress bar will only be possible once the user clicks on the Button provided on the screen. Here, you will get a step-to-step guide to develop an Android application with the provision of a progress bar.
Step 1: Create an Android application with the name myapplication. This is to be done on the Android Studio IDE. Store this app under the com.example.w3school.myapplication package.
Step 2: Now, update the given code in the MainActivity.java file. To find this file, look into the src/ folder. In this file, you are adding progress code as to display the progress dialog on the screen. Main activity file also contains the fundamental lifecycle callback methods as its default content.
package com.example.w3school.myapplication;
import android.app.ProgressDialog;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.ProgressDialog;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity
{
Button b1;
private ProgressDialog progress;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button2);
}
{
Button b1;
private ProgressDialog progress;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button2);
}
[post_middile_section_ad]
public void download(View view)
{
progress=new ProgressDialog(this);
progress.setMessage(“Downloading Music”);
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress.setIndeterminate(true);
progress.setProgress(0);
progress.show();
final int totalProgressTime = 100;
final Thread t = new Thread()
{
@Override
public void run()
{
int jumpTime = 0;
while(jumpTime < totalProgressTime)
{
try
{
sleep(200);
jumpTime += 5;
progress.setProgress(jumpTime);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
t.start();
}
}
{
progress=new ProgressDialog(this);
progress.setMessage(“Downloading Music”);
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress.setIndeterminate(true);
progress.setProgress(0);
progress.show();
final int totalProgressTime = 100;
final Thread t = new Thread()
{
@Override
public void run()
{
int jumpTime = 0;
while(jumpTime < totalProgressTime)
{
try
{
sleep(200);
jumpTime += 5;
progress.setProgress(jumpTime);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
t.start();
}
}
Step 3: Another file which needs modification is activity_main.xml. Add the following XML code to this file. You can easily identify this file after following the res/layout/ directory.
<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:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
android:paddingBottom=”@dimen/activity_vertical_margin” tools:context=”.MainActivity”>
xmlns:tools=”http://schemas.android.com/tools” android:layout_width=”match_parent”
android:layout_height=”match_parent” android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
android:paddingBottom=”@dimen/activity_vertical_margin” tools:context=”.MainActivity”>
<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/textView”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:textSize=”30dp”
android:text=”Progress bar” />
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/textView”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:textSize=”30dp”
android:text=”Progress bar” />
<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”W3School”
android:id=”@+id/textView2″
android:layout_below=”@+id/textView”
android:layout_centerHorizontal=”true”
android:textSize=”35dp”
android:textColor=”#ff16ff01″ />
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”W3School”
android:id=”@+id/textView2″
android:layout_below=”@+id/textView”
android:layout_centerHorizontal=”true”
android:textSize=”35dp”
android:textColor=”#ff16ff01″ />
<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Download”
android:onClick=”download”
android:id=”@+id/button2″
android:layout_marginLeft=”125dp”
android:layout_marginStart=”125dp”
android:layout_centerVertical=”true” />
</RelativeLayout>
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Download”
android:onClick=”download”
android:id=”@+id/button2″
android:layout_marginLeft=”125dp”
android:layout_marginStart=”125dp”
android:layout_centerVertical=”true” />
</RelativeLayout>
Step 4: Let the AndroidManifest.xml file remain as it is.
Android Horizontal Progress Bar Thickness Sheet
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.w3school.myapplication” >
<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.w3school.myapplication” >
<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
Android Horizontal Progress Bar Thickness Tool
[post_middile_section_ad]
<activity
android:name=”.MainActivity”
android:label=”@string/app_name” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>
</manifest>
android:name=”.MainActivity”
android:label=”@string/app_name” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>
</manifest>
Android Horizontal Progress Bar Thickness Gauge
Step 5: Now, run this app in the Android Studio. For that, you will be required to have an AVD (must have created during the environment setup). Open one of the activity files from your project and click on the Run button. Before the process starts, you will get the following dialog box, asking you to choose one of the available options.
Select your mobile device and you will be displayed with the final output on your device. Now, the screen is having a “DOWNLOAD” button to offer. Once the user clicks on it, he/she will be displayed with a progress bar labeled as “Downloading Music”.