Progress bar is used to show the progress of a task. E.g. 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 a progress bar. To do this, you need to instantiate an object of this class. Its syntax is as follows: - ProgressDialog progress = new ProgressDialog( this );
Now, you can set some properties of this dialog box. For example, its style, text, etc. - progress.setMessage( "Downloading Music :) " );
- progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
- progress.setIndeterminate( true );
In addition to these methods, the ProgressDialog class also provides other methods: Sr. NO | Title and description |
---|
1 | getMax() This method returns the maximum value of the progress | 2 | incrementProgressBy(int diff) This method adds a distinction to the progress bar by the value passed as parameter | 3 | setIndeterminate(boolean indeterminate) This method sets the progress indicator to determinate or indeterminate | 4 | setMax(int max) This method sets the maximum value of the progress dialog box. | 5 | setProgress(int value) This method is used to update certain specific values of the dialog progress | 6 | show(Context context, CharSequence title, CharSequence message) This is a static method used to display a progress dialog box. |
Example This example illustrates the use of dialog horizontal progress, which is actually a progress bar. It displays the progress bar when a button is pressed. To test this example, you need to follow the steps below to develop the application and run it on an actual device. Steps | describe |
---|
1 | Use Android Studio to create an Android application and name it ProgressDialogDemo. When creating this project, make sure the target SDK and compile to the latest version of the Android SDK and use higher-level APIs. | 2 | Modify the src/MainActivity.java file and add progress code to display the dialog progress | 3 | Modify the res/layout/activity_main.xml file and add the corresponding XML code | 4 | Modify the res/values/string.xml file and add a message as a string constant | 5 | Run the app and select the running Android device and install the app on it and verify the result. |
The following is the content of the modified main activity file src/com.yiibai.progressdialog/MainActivity.java. - package com.example.progressdialog;
-
- import com.example.progressdialog.R;
-
- import android.os.Bundle;
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.view.Menu;
- import android.view.View;
-
- public class MainActivity extends Activity {
-
- private ProgressDialog progress;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super .onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- progress = new ProgressDialog( this );
- }
-
-
- public void open(View view){
- progress.setMessage( "Downloading Music :) " );
- progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
-
- 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) {
-
- e.printStackTrace();
- }
-
- }
-
- }
- };
- t.start();
-
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
- getMenuInflater().inflate(R.menu.main, menu);
- return true ;
- }
- }
Modify the content of res/layout/activity_main.xml as follows - <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= ".MainActivity" >
-
- <Button
- android:id= "@+id/button1"
- android:layout_width= "wrap_content"
- android:layout_height= "wrap_content"
- android:layout_alignParentTop= "true"
- android:layout_centerHorizontal= "true"
- android:layout_marginTop= "150dp"
- android:onClick= "open"
- android:text= "@string/download_button" />
-
- <TextView
- android:id= "@+id/textView1"
- android:layout_width= "wrap_content"
- android:layout_height= "wrap_content"
- android:layout_alignParentRight= "true"
- android:layout_alignParentTop= "true"
- android:layout_marginTop= "19dp"
- android:text= "@string/download_text"
- android:textAppearance= "?android:attr/textAppearanceLarge" />
-
- </RelativeLayout>
Modify res/values/string.xml to the following content - <? xml version = "1.0" encoding = "utf-8" ?>
- < resources >
- < string name = "app_name" > ProgressDialog </ string >
- < string name = "action_settings" > Settings </ string >
- < string name = "hello_world" > Hello world! </ string >
- < string name = "download_button" > Download </ string >
- < string name = "download_text" > Press the button to download music </ string >
- </ resources >
This is the default AndroidManifest.xml file - <? xml version = "1.0" encoding = "utf-8" ?>
- < manifest xmlns:android = "http://schemas.android.com/apk/res/android"
- package = "com.yiibai.progressdialog"
- android:versionCode = "1"
- android:versionName = "1.0" >
-
- < uses-sdk
- android:minSdkVersion = "8"
- android:targetSdkVersion = "17" />
-
- < application
- android:allowBackup = "true"
- android:icon = "@drawable/ic_launcher"
- android:label = "@string/app_name"
- android:theme = "@style/AppTheme" >
- < activity
- android:name = "com.yiibai.progressdialog.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 >
Let's try running the ProgressDialogDemo application. Assuming you have connected an actual Android mobile device to your computer. Before launching the application, the following window will be displayed with the option to select the Android application to run. Select Mobile Device as an option and then view the Mobile Device display as follows: Just press the button to start the progress bar. After pressing it, the following screen will be displayed: It will continue to update itself, and after a few seconds, the following image will appear: Download sample code: http://pan.baidu.com/s/1qW9IElQ |