Progress bar example ProgressDialog in Android

Progress bar example ProgressDialog in Android

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:

  1. ProgressDialog progress = new ProgressDialog( this );

Now, you can set some properties of this dialog box. For example, its style, text, etc.

  1. progress.setMessage( "Downloading Music :) " );
  2. progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  3. 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.

  1. package com.example.progressdialog;
  2.  
  3. import com.example.progressdialog.R;
  4.  
  5. import android.os.Bundle;
  6. import android.app.Activity;
  7. import android.app.ProgressDialog;
  8. import android.view.Menu;
  9. import android.view.View;
  10.  
  11. public   class MainActivity extends Activity {
  12.  
  13. private ProgressDialog progress;
  14. @Override  
  15. protected   void onCreate(Bundle savedInstanceState) {
  16. super .onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. progress = new ProgressDialog( this );
  19. }
  20.  
  21.  
  22. public   void open(View view){
  23. progress.setMessage( "Downloading Music :) " );
  24. progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  25. //progress.setIndeterminate(true);  
  26. progress.show();
  27.  
  28. final   int totalProgressTime = 100 ;
  29.  
  30. final Thread t = new Thread(){
  31.  
  32. @Override  
  33. public   void run(){
  34.   
  35. int jumpTime = 0 ;
  36. while (jumpTime < totalProgressTime){
  37. try {
  38. sleep( 200 );
  39. jumpTime += 5 ;
  40. progress.setProgress(jumpTime);
  41. } catch (InterruptedException e) {
  42. // TODO Auto-generated catch block  
  43. e.printStackTrace();
  44. }
  45.  
  46. }
  47.  
  48. }
  49. };
  50. t.start();
  51.  
  52. }
  53. @Override  
  54. public   boolean onCreateOptionsMenu(Menu menu) {
  55. // Inflate the menu; this adds items to the action bar if it is present.  
  56. getMenuInflater().inflate(R.menu.main, menu);
  57. return   true ;
  58. }
  59. }

Modify the content of res/layout/activity_main.xml as follows

  1. <RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android"  
  2. xmlns:tools= "http://schemas.android.com/tools"  
  3. android:layout_width= "match_parent"  
  4. android:layout_height= "match_parent"  
  5. android:paddingBottom= "@dimen/activity_vertical_margin"  
  6. android:paddingLeft= "@dimen/activity_horizontal_margin"  
  7. android:paddingRight= "@dimen/activity_horizontal_margin"  
  8. android:paddingTop= "@dimen/activity_vertical_margin"  
  9. tools:context= ".MainActivity" >
  10.  
  11. <Button
  12. android:id= "@+id/button1"  
  13. android:layout_width= "wrap_content"  
  14. android:layout_height= "wrap_content"   
  15. android:layout_alignParentTop= "true"  
  16. android:layout_centerHorizontal= "true"  
  17. android:layout_marginTop= "150dp"  
  18. android:onClick= "open"  
  19. android:text= "@string/download_button" />
  20.  
  21. <TextView
  22. android:id= "@+id/textView1"  
  23. android:layout_width= "wrap_content"  
  24. android:layout_height= "wrap_content"  
  25. android:layout_alignParentRight= "true"  
  26. android:layout_alignParentTop= "true"  
  27. android:layout_marginTop= "19dp"  
  28. android:text= "@string/download_text"  
  29. android:textAppearance= "?android:attr/textAppearanceLarge" />
  30.  
  31. </RelativeLayout>

Modify res/values/string.xml to the following content

  1. <? xml   version = "1.0"   encoding = "utf-8" ?>  
  2. < resources >  
  3. < string   name = "app_name" > ProgressDialog </ string >  
  4. < string   name = "action_settings" > Settings </ string >  
  5. < string   name = "hello_world" > Hello world! </ string >  
  6. < string   name = "download_button" > Download </ string >  
  7. < string   name = "download_text" > Press the button to download music </ string >  
  8. </ resources >  

This is the default AndroidManifest.xml file

  1. <? xml   version = "1.0"   encoding = "utf-8" ?>  
  2. < manifest   xmlns:android = "http://schemas.android.com/apk/res/android"  
  3. package = "com.yiibai.progressdialog"  
  4. android:versionCode = "1"  
  5. android:versionName = "1.0"   >  
  6.  
  7. < uses-sdk  
  8. android:minSdkVersion = "8"  
  9. android:targetSdkVersion = "17"   />  
  10.  
  11. < application  
  12. android:allowBackup = "true"  
  13. android:icon = "@drawable/ic_launcher"  
  14. android:label = "@string/app_name"  
  15. android:theme = "@style/AppTheme"   >  
  16. < activity  
  17. android:name = "com.yiibai.progressdialog.MainActivity"  
  18. android:label = "@string/app_name"   >  
  19. <intent-filter>  
  20. < action   android:name = "android.intent.action.MAIN"   />  
  21.  
  22. < category   android:name = "android.intent.category.LAUNCHER"   />  
  23. </intent-filter>  
  24. </ activity >  
  25. </ application >  
  26.  
  27. </ 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

<<:  The most comprehensive iOS language learning materials collection

>>:  Apple Swift language open source project selection summary

Recommend

Wuwei Investment Class "Buy at the Tipping Point"

Wuwei Investment Classroom "Buy at the Tippi...

When traveling on vacation, please keep this list of essential medicines!

Entering the Mid-Autumn Festival and National Day...

WeChat upgraded with new features: My three-year-old phone is no longer stuck!

Regardless of the model, high configuration or lo...

How difficult is it to prevent floods in the Yangtze River?

↑A group of National Geographic fans, focusing on...

These 4 strategies for promoting Xiaohongshu are a must-read!

Today, Xiaohongshu is increasingly becoming a bat...

How to create a “hit product”?

Introduction: As long as the trend is right, ever...

Apple mobile phone marketing strategy!

An excellent brand is inseparable from excellent ...