{"id":2604,"date":"2016-04-16T06:49:16","date_gmt":"2016-04-16T06:49:16","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2604"},"modified":"2016-04-16T09:10:19","modified_gmt":"2016-04-16T09:10:19","slug":"how-to-show-a-progress-dialog-in-android","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2016\/04\/16\/how-to-show-a-progress-dialog-in-android\/","title":{"rendered":"How To Show A Progress Dialog in Android"},"content":{"rendered":"            <script type=\"text\/javascript\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/plugins\/wordpress-code-snippet\/scripts\/shBrushJava.js\"><\/script>\n<h4>OVERVIEW<\/h4>\n<p>When you need to do some long running task and show a &#8220;wait&#8221; dialog to the user while the task is running, a Progress Dialog is the best way to go. When you do not know how long the task will take , then you show a Spinner instead of a horizontal progress bar. Here we will use the example of using a Spinner while authenticating a user after he has filled in a login form.<\/p>\n<p>&nbsp;<\/p>\n<h4>CODE<\/h4>\n<p>In our layout xml we called the doLogin() function when the Login button is clicked. This function will in turn execute an AsyncTask which will execute a backend process. The ProgressDialog will be shown before the AsyncTask starts execution and will go away once the AsyncTask has finished execution.<br \/>\n<pre class=\"brush: java\">public class LoginActivity extends ActionBarActivity {\r\n\r\n    ProgressDialog mProgressBar;\r\n\r\n @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_login);\r\n\r\n        mProgressBar = new ProgressDialog(this);\r\n    }\r\n\r\n public void doLogin(View view) {\r\n        mProgressBar.setCancelable(false);\r\n        mProgressBar.setMessage(&quot;Authenticating your login..&quot;);\r\n        mProgressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);\r\n        mProgressBar.show();\r\n        new LoginTask().execute(0);\r\n\r\n    }\r\n}\r\n\r\n  \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\r\n    \/\/\r\n    \/\/ The params are dummy and not used\r\n    \/\/\r\n    class LoginTask extends AsyncTask&lt;Integer, Integer, String&gt; {\r\n        @Override\r\n        protected String doInBackground(Integer... params) {\r\n\r\n                try {\r\n                    Thread.sleep(2000);\r\n                    \/\/ some long running task will run here. We are using sleep as a dummy to delay execution\r\n\r\n                } catch (InterruptedException e) {\r\n                    e.printStackTrace();\r\n                }\r\n\r\n            return &quot;Task Completed.&quot;;\r\n        }\r\n        @Override\r\n        protected void onPostExecute(String result) {\r\n            mProgressBar.hide();\r\n            \/\/ do whatever needs to be done next\r\n        }\r\n        @Override\r\n        protected void onPreExecute() {\r\n        }\r\n        @Override\r\n        protected void onProgressUpdate(Integer... values) {\r\n\r\n        }\r\n    }<\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW When you need to do some long running task and show a &#8220;wait&#8221; dialog to the user while the task is running, a Progress <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2016\/04\/16\/how-to-show-a-progress-dialog-in-android\/\" title=\"How To Show A Progress Dialog in Android\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":2608,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[295],"tags":[],"class_list":["post-2604","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android-dev"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2604","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=2604"}],"version-history":[{"count":4,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2604\/revisions"}],"predecessor-version":[{"id":2609,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2604\/revisions\/2609"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/2608"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=2604"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2604"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2604"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}