{"id":2394,"date":"2017-03-23T04:28:19","date_gmt":"2017-03-23T04:28:19","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?page_id=2394"},"modified":"2019-06-14T11:02:06","modified_gmt":"2019-06-14T11:02:06","slug":"progressdialog","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/ui\/progressdialog","title":{"rendered":"ProgressDialog Tutorial With Example In Android Studio"},"content":{"rendered":"<p>Android Progress Dialog is a UI which shows the progress of a task like you want user to wait until the previous lined up task is completed and for that purpose you can use progress dialog. The best example is you see when\u00a0downloading or uploading any file.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2414\" src=\"\/ui\/wp-content\/uploads\/2017\/03\/ProgressDialog-Example-In-Android-Studio.gif\" alt=\"ProgressDialog Example In Android Studio\" width=\"266\" height=\"419\" \/><\/p>\n<p>In this tutorial I will\u00a0demonstrate both the Ring and the Bar style(Horizontal) ProgressDialog\u00a0component and will also\u00a0use the special tools like Handler to update the value of the bar format.<\/p>\n<pre>  ProgressDialog progressDialog = new ProgressDialog(this);<\/pre>\n<h4><strong>Important Methods Of ProgressDialog<\/strong><\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2405\" src=\"\/ui\/wp-content\/uploads\/2017\/03\/Progress-Dialog-Methods-In-Android-Studio.png\" alt=\"Progress Dialog Methods In Android Studio\" width=\"306\" height=\"418\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2017\/03\/Progress-Dialog-Methods-In-Android-Studio.png 306w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2017\/03\/Progress-Dialog-Methods-In-Android-Studio-220x300.png 220w\" sizes=\"auto, (max-width: 306px) 100vw, 306px\" \/><br \/>\n<span style=\"color: #008000;\"><strong>1. setTitle(CharSequence title) \u2013<\/strong> <\/span>This component is used to set the title of the progress dialog.<\/p>\n<pre>\/\/ Setting Title\r\nprogressDialog.setTitle(\"ProgressDialog\"); \r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>2. setMessage(CharSequence message) &#8211;<\/strong> <\/span>This component displays the required message in the progress dialog.<\/p>\n<pre>\/\/ Setting Message\r\nprogressDialog.setMessage(\"Loading...\"); \r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>3. setProgressStyle(ProgressDialog.STYLE_HORIZONTAL) &#8211;<\/strong> <\/span>This is used for setting the horizontal style of the progress dialog.<\/p>\n<pre>\/\/ Progress Dialog Style Horizontal\r\nprogressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);<\/pre>\n<p><strong><span style=\"color: #008000;\">4. setProgressStyle(ProgressDialog.STYLE_SPINNER) &#8211;<\/span> <\/strong>This is used for setting the spinner style of the progress dialog.<\/p>\n<pre>\/\/ Progress Dialog Style Spinner\r\nprogressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); \r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2406\" src=\"\/ui\/wp-content\/uploads\/2017\/03\/Progress-Dialog-Style-Spinner-In-Android-Studio.png\" alt=\"Progress Dialog Style Spinner In Android Studio\" width=\"304\" height=\"254\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2017\/03\/Progress-Dialog-Style-Spinner-In-Android-Studio.png 304w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2017\/03\/Progress-Dialog-Style-Spinner-In-Android-Studio-300x251.png 300w\" sizes=\"auto, (max-width: 304px) 100vw, 304px\" \/><br \/>\n<strong><span style=\"color: #008000;\">5. setMax(int max) &#8211;<\/span><\/strong> This method sets the maximum value of the progress dialog.<\/p>\n<pre>\/\/ Progress Dialog Max Value\r\nprogressDialog.setMax(100);<\/pre>\n<p><strong><span style=\"color: #008000;\">6. getMax() &#8211;<\/span> <\/strong>This method return the maximum value of the progress dialog, basically this method is used while applying condition over the progress dialog.<\/p>\n<pre>\/\/ Fetching max value\r\nprogressDialog.getMax();<\/pre>\n<p><strong><span style=\"color: #008000;\">7. getProgess() &#8211;<\/span><\/strong> This returns current progress of the progress dialog in numeric.<\/p>\n<pre>\/\/ Fetching current progress\r\nprogressDialog.getProgress();<\/pre>\n<p><strong><span style=\"color: #008000;\">8. incrementProgressBy(int diff) &#8211;<\/span><\/strong> This method increments the progress dialog value with the defined value.<\/p>\n<pre> \/\/ Incremented By Value 2\r\nprogressDialog.incrementProgressBy(2); \r\n<\/pre>\n<p><strong><span style=\"color: #008000;\">9. setCancelable(boolean cancelable) &#8211;<\/span><\/strong> This method has boolean value i.e true\/false. If set to false it allows to cancel the dialog box by clicking on area outside the dialog default it is true if method is not used.<\/p>\n<pre>\/\/ Cannot Cancel Progress Dialog\r\nprogressDialog.setCancelable(false); \r\n<\/pre>\n<p><strong><span style=\"color: #008000;\">10. dismiss() &#8211;<\/span><\/strong> This method dismiss the progressdialog.<\/p>\n<pre>\/\/Dismiss the dialog\r\nprogressDialog.dismiss();\r\n<\/pre>\n<hr \/>\n<h4><strong>ProgressDialog Example In Android Studio:<\/strong><\/h4>\n<p>Below is the example of Progress Dialog in which the functionality of ProgressDialog is defined over the buttons click. In this example we have used a simple buttons and over that button click the ProgressDialog will appear.<\/p>\n<p>Below you can download code, see final output and step by step explanation of ProgressDialog example in Android Studio.<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"http:\/\/www.mediafire.com\/file\/ocz3n0z1qv1952d\/ProgessDialogExample.zip\/file\" target=\"_blank\" rel=\"nofollow\">Download Code<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2414\" src=\"\/ui\/wp-content\/uploads\/2017\/03\/ProgressDialog-Example-In-Android-Studio.gif\" alt=\"ProgressDialog Example In Android Studio\" width=\"266\" height=\"419\" \/><br \/>\n<span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> Create a new project and name it <strong>ProgressDialogExample<\/strong>.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Open <span style=\"color: #008000;\">res -&gt; layout -&gt; activity_main.xml (or) main.xml<\/span> and add following code:<\/p>\n<p>Here define two buttons<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:id=\"@+id\/activity_main\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:paddingBottom=\"@dimen\/activity_vertical_margin\"\r\n    android:paddingLeft=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingRight=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingTop=\"@dimen\/activity_vertical_margin\"\r\n    tools:context=\"com.example.progessdialogexample.MainActivity\"\r\n    android:elevation=\"1dp\"&gt;\r\n\r\n    &lt;Button\r\n        android:text=\"  Click To View Ring Progress Dialog...\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_marginTop=\"171dp\"\r\n        android:id=\"@+id\/button\"\r\n        style=\"@android:style\/Widget.DeviceDefault.Button.Inset\"\r\n        android:textStyle=\"normal|bold\"\r\n        android:textSize=\"15sp\"\r\n        android:textColor=\"@android:color\/background_dark\"\r\n        android:layout_alignParentTop=\"true\"\r\n        android:layout_centerHorizontal=\"true\" \/&gt;\r\n\r\n    &lt;Button\r\n        android:text=\"  Click To View Progress Dialog...\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        style=\"@android:style\/Widget.DeviceDefault.Button.Inset\"\r\n        android:layout_marginTop=\"49dp\"\r\n        android:textSize=\"15sp\"\r\n        android:id=\"@+id\/button2\"\r\n        android:textStyle=\"normal|bold\"\r\n        android:textColor=\"@android:color\/background_dark\"\r\n        android:layout_below=\"@+id\/button\"\r\n        android:layout_centerHorizontal=\"true\" \/&gt;\r\n&lt;\/RelativeLayout&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3 :<\/strong><\/span> Now open <span style=\"color: #008000;\">app -&gt; java -&gt; package -&gt; MainActivity.java<\/span> and add the below code.<\/p>\n<p>In this we added the progress dialog functionality on the button click having onclicklistener. \u00a0The two buttons both are displaying different type of progress dialog which is set using setProgressStyle i.e spinner or horizontal bar.<\/p>\n<pre>package com.example.progessdialogexample;\r\n\r\nimport android.app.ProgressDialog;\r\nimport android.os.Handler;\r\nimport android.os.Message;\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.os.Bundle;\r\nimport android.view.View;\r\nimport android.widget.Button;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n    Button b1, b2;\r\n    ProgressDialog progressDialog;\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        b1 = (Button) findViewById(R.id.button);\r\n        b2 = (Button) findViewById(R.id.button2);\r\n\r\n        b1.setOnClickListener(new View.OnClickListener() {\r\n            @Override\r\n            public void onClick(View v) {\r\n                progressDialog = new ProgressDialog(MainActivity.this);\r\n                progressDialog.setMessage(\"Loading...\"); \/\/ Setting Message\r\n                progressDialog.setTitle(\"ProgressDialog\"); \/\/ Setting Title\r\n                progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); \/\/ Progress Dialog Style Spinner\r\n                progressDialog.show(); \/\/ Display Progress Dialog\r\n                progressDialog.setCancelable(false);\r\n                new Thread(new Runnable() {\r\n                    public void run() {\r\n                        try {\r\n                            Thread.sleep(10000);\r\n                        } catch (Exception e) {\r\n                            e.printStackTrace();\r\n                        }\r\n                        progressDialog.dismiss();\r\n                    }\r\n                }).start();\r\n            }\r\n        });\r\n\r\n        b2.setOnClickListener(new View.OnClickListener() {\r\n            Handler handle = new Handler() {\r\n                public void handleMessage(Message msg) {\r\n                    super.handleMessage(msg);\r\n                    progressDialog.incrementProgressBy(2); \/\/ Incremented By Value 2\r\n                }\r\n            };\r\n\r\n            @Override\r\n            public void onClick(View v) {\r\n                progressDialog = new ProgressDialog(MainActivity.this);\r\n                progressDialog.setMax(100); \/\/ Progress Dialog Max Value\r\n                progressDialog.setMessage(\"Loading...\"); \/\/ Setting Message\r\n                progressDialog.setTitle(\"ProgressDialog\"); \/\/ Setting Title\r\n                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); \/\/ Progress Dialog Style Horizontal\r\n                progressDialog.show(); \/\/ Display Progress Dialog\r\n                progressDialog.setCancelable(false);\r\n                new Thread(new Runnable() {\r\n                    @Override\r\n                    public void run() {\r\n                        try {\r\n                            while (progressDialog.getProgress() &lt;= progressDialog.getMax()) {\r\n                                Thread.sleep(200);\r\n                                handle.sendMessage(handle.obtainMessage());\r\n                                if (progressDialog.getProgress() == progressDialog.getMax()) {\r\n                                    progressDialog.dismiss();\r\n                                }\r\n                            }\r\n                        } catch (Exception e) {\r\n                            e.printStackTrace();\r\n                        }\r\n                    }\r\n                }).start();\r\n            }\r\n        });\r\n    }\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<p>Now start the AVD in Emulator and run the App. You will see two button. Click on them and see the different ProgressDialog in android.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Android Progress Dialog is a UI which shows the progress of a task like you want user to wait until the previous lined up task is completed and for that purpose you can use progress dialog. The best example is you see when\u00a0downloading or uploading any file. In this tutorial I will\u00a0demonstrate both the Ring &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/progressdialog\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">ProgressDialog Tutorial With Example In Android Studio<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"_acf_changed":false,"footnotes":""},"class_list":["post-2394","page","type-page","status-publish","hentry"],"acf":[],"psp_head":"<title>ProgressDialog Tutorial With Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Android Progress Dialog is a UI which shows the progress of a task like you want user to wait until the previous lined up task is completed and for that purpose you can use progress dialog.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/progressdialog\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/2394","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/comments?post=2394"}],"version-history":[{"count":2,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/2394\/revisions"}],"predecessor-version":[{"id":2849,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/2394\/revisions\/2849"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=2394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}