{"id":988,"date":"2016-02-29T06:44:34","date_gmt":"2016-02-29T06:44:34","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?page_id=988"},"modified":"2019-06-12T10:01:07","modified_gmt":"2019-06-12T10:01:07","slug":"progressbar","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/ui\/progressbar","title":{"rendered":"ProgressBar Tutorial With Example In Android Studio"},"content":{"rendered":"<p>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. It mainly use the<strong>\u00a0&#8220;android.widget.ProgressBar&#8221;\u00a0<\/strong>class.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1023\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/ProgressBar-in-Android.jpg\" alt=\"ProgressBar in Android\" width=\"484\" height=\"176\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/ProgressBar-in-Android.jpg 484w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/ProgressBar-in-Android-300x109.jpg 300w\" sizes=\"auto, (max-width: 484px) 100vw, 484px\" \/><\/center><strong><span style=\"color: #ff0000;\">Important Note:<\/span>\u00a0<\/strong>A progress bar can also be made indeterminate. In this mode a progress bar shows a cyclic animation without an indication of progress. This mode is used in application when we don\u2019t know the amount of work to be done.<\/p>\n<p>To add a progress bar to a layout (xml) file, you can use the\u00a0&lt;ProgressBar&gt;\u00a0element. By default, a progress bar is a spinning wheel (an indeterminate indicator). To change to a horizontal progress bar, apply the progress bar\u2019s horizontal style.<\/p>\n<p><strong>ProgressBar code:<\/strong><\/p>\n<pre>&lt;ProgressBar\r\nandroid:id=\"@+id\/simpleProgressBar\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\" \/&gt;\r\n<\/pre>\n<p><strong>Horizontal ProgressBar code:<\/strong><\/p>\n<pre>&lt;ProgressBar\r\nandroid:id=\"@+id\/simpleProgressBar\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nstyle=\"@style\/Widget.AppCompat.ProgressBar.Horizontal\"\/&gt;\r\n<\/pre>\n<hr \/>\n<h4><strong>Important\u00a0Methods Used In ProgressBar:<\/strong><\/h4>\n<p><span style=\"color: #008000;\"><strong>1. getMax() &#8211; returns the maximum value of progress bar<\/strong><\/span><\/p>\n<p>We can get the maximum value of the progress bar in java class. This method returns a integer value. Below is the code to get the maximum value from a Progress bar.<\/p>\n<pre>ProgressBar simpleProgressBar=(ProgressBar) findViewById(R.id.<em>simpleProgressBar<\/em>); \/\/ initiate the progress bar \r\nint maxValue=simpleProgressBar.getMax(); \/\/ get maximum value of the progress bar\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>2. getProgress() &#8211; returns current progress value<\/strong><\/span><\/p>\n<p>We can get the current progress value from a progress bar in java class. This method also returns a integer value. Below\u00a0is the code to\u00a0get current progress value from a Progress bar.<\/p>\n<pre>ProgressBar simpleProgressBar=(ProgressBar)findViewById(R.id.<em>simpleProgressBar<\/em>); \/\/ initiate the progress bar\r\nint progressValue=simpleProgressBar.getProgress(); \/\/ get progress value from the progress bar\r\n<\/pre>\n<hr \/>\n<h4><strong>Attributes of ProgressBar In Android:<\/strong><\/h4>\n<p>Now let\u2019s discuss important\u00a0attributes that helps us to configure a Progress bar in xml file (layout).<\/p>\n<p><strong><span style=\"color: #008000;\">1. id:<\/span>\u00a0<\/strong>id is an attribute used to uniquely identify a Progress bar.<\/p>\n<pre>&lt;ProgressBar\r\n<strong>android:id=\"@+id\/simpleProgressBar\"<\/strong> \r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nstyle=\"@style\/Widget.AppCompat.ProgressBar.Horizontal\"\/&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>2. max: <\/strong><\/span>max is an attribute used in android to define maximum value of the progress can take. It must be an integer value like 100, 200 etc.<\/p>\n<p>Below we set 100 maximum value for a progress bar.<\/p>\n<pre>&lt;ProgressBar\r\n    android:id=\"@+id\/simpleProgressBar\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    style=\"@style\/Widget.AppCompat.ProgressBar.Horizontal\"\r\n    android:max=\"100\" \/&gt;&lt;!--set 100 maximum value for the progress bar--&gt;<\/pre>\n<p><strong>Set Max Value\u00a0of ProgressBar In Java Class :<\/strong><\/p>\n<pre>ProgressBar simpleProgressBar=(ProgressBar) findViewById(R.id.<strong><em>simpleProgressBar<\/em><\/strong>); \/\/ initiate the progress bar\r\nsimpleProgressBar.setMax(100); \/\/ 100 maximum value for the progress bar\r\n<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1017\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/max-in-ProgressBar-Android.jpg\" alt=\"max in ProgressBar Android\" width=\"229\" height=\"209\" \/><\/center><span style=\"color: #008000;\"><strong>3. progress: <\/strong><\/span>progress is an attribute used in android to define the default progress value between 0 and max. It must be an integer value.<\/p>\n<p>Below we set the 100 max value and then set 50 default progress.<\/p>\n<pre>&lt;ProgressBar\r\n    android:id=\"@+id\/simpleProgressBar\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:max=\"100\"\r\n    style=\"@style\/Widget.AppCompat.ProgressBar.Horizontal\"\r\n    android:progress=\"50\"\/&gt;&lt;!--\/\/ 50 default progress value--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1018\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/progress-in-ProgressBar-Android.jpg\" alt=\"progress in ProgressBar Android\" width=\"233\" height=\"219\" \/><\/center><strong>Setting Progress Value of ProgressBar In Java Class :<\/strong><\/p>\n<pre>ProgressBar simpleProgressBar=(ProgressBar)findViewById(R.id.<strong><em>simpleProgressBar<\/em><\/strong>); \/\/ initiate the progress bar\r\nsimpleProgressBar.setMax(100); \/\/ 100 maximum value for the progress value\r\nsimpleProgressBar.setProgress(50); \/\/ 50 default progress value for the progress bar\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>4. progressDrawable: <\/strong><\/span>progress drawable is an attribute used in Android to set the custom drawable for the progress mode.<\/p>\n<p>Below we set a custom gradient drawable for the progress mode of a progress bar.\u00a0Before you try below code make sure to download a progress icon from the web and add in your drawable folder.<\/p>\n<p><strong>Step 1:<\/strong> Add this code in activity_main.xml or main.xml inside layout.<\/p>\n<pre>&lt;ProgressBar\r\nandroid:id=\"@+id\/simpleProgressBar\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:max=\"100\"\r\nandroid:progress=\"60\"\r\nandroid:layout_marginTop=\"100dp\"\r\nstyle=\"@style\/Widget.AppCompat.ProgressBar.Horizontal\"\r\nandroid:progressDrawable=\"@drawable\/custom_progress\"\/&gt;&lt;!--custom progress drawable for progress mode--&gt;\r\n<\/pre>\n<p><strong>Step 2:<\/strong> Create a new drawable resource xml in drawable folder and name it custom_progress. Here add the below code which creates gradient effect in progressbar.<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;layer-list xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\" &gt;\r\n\r\n    &lt;item&gt;\r\n        &lt;shape&gt;\r\n            &lt;gradient\r\n                android:endColor=\"#fff\"\r\n                android:startColor=\"#1f1\"\r\n                android:useLevel=\"true\" \/&gt;\r\n\r\n        &lt;\/shape&gt;\r\n    &lt;\/item&gt;\r\n\r\n\r\n&lt;\/layer-list&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1019\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/progressDrawable-in-ProgressBar-Android.jpg\" alt=\"progressDrawable in ProgressBar Android\" width=\"233\" height=\"208\" \/><\/center><span style=\"color: #008000;\"><strong>5. background:<\/strong><\/span>\u00a0background attribute is used to set the background of a Progress bar. We can set a color or a drawable in the background of a Progress bar.<\/p>\n<p>Below we set the black color for the background of a Progress bar.<\/p>\n<pre>&lt;ProgressBar\r\n    android:id=\"@+id\/simpleProgressBar\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:max=\"100\"\r\n    android:progress=\"50\"\r\n    style=\"@style\/Widget.AppCompat.ProgressBar.Horizontal\"\r\n    android:background=\"#000\"\/&gt;&lt;!-- black background color for progress bar--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1020\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/background-in-ProgressBar.jpg\" alt=\"background in ProgressBar\" width=\"234\" height=\"171\" \/><\/center><span style=\"color: #008000;\"><strong>6. indeterminate:<\/strong><\/span>\u00a0indeterminate attribute is used in Android\u00a0to\u00a0enable the indeterminate mode.\u00a0In this mode a progress bar shows a cyclic animation without an indication of progress. This mode is used in application when we don\u2019t know the amount of work to be done. In this mode the actual working will not be shown.<\/p>\n<p>In below code we set the indeterminate to true.<\/p>\n<pre>&lt;ProgressBar\r\n    android:id=\"@+id\/simpleProgressBar\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:max=\"100\"\r\n    android:progress=\"50\"\r\n    android:background=\"#000\"\r\n    android:padding=\"20dp\" style=\"@style\/Widget.AppCompat.ProgressBar.Horizontal\"\r\n    android:indeterminate=\"true\"\/&gt;&lt;!--true value for indeterminate--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1021\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/indeterminate-in-ProgressBar-Android.jpg\" alt=\"indeterminate in ProgressBar Android\" width=\"293\" height=\"283\" \/><\/center><strong>Setting indeterminate of ProgressBar In Java class:<\/strong><\/p>\n<pre>ProgressBar simpleProgressBar=(ProgressBar)findViewById(R.id.<strong><em>simpleProgressBar<\/em><\/strong>); \/\/ initiate the progress bar\r\nsimpleProgressBar.setBackgroundColor(Color.<strong><em>BLACK<\/em><\/strong>); \/\/ black background color for the progress bar\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>7.\u00a0<\/strong><strong style=\"line-height: 1.6471;\">padding:<\/strong><\/span><span style=\"line-height: 1.6471;\">\u00a0padding attribute is used to set the padding from left, right, top or bottom of ProgressBar.<\/span><\/p>\n<ul>\n<li><strong>paddingRight:<\/strong>\u00a0set the padding from the right side of the Progress bar<strong>.<\/strong><\/li>\n<li><strong>paddingLeft:<\/strong>\u00a0set the padding from the left side of the Progress bar<strong>.<\/strong><\/li>\n<li><strong>paddingTop:<\/strong>\u00a0set the padding from the top side of the Progress bar<strong>.<\/strong><\/li>\n<li><strong>paddingBottom:<\/strong>\u00a0set the padding from the bottom side of the Progress bar<strong>.<\/strong><\/li>\n<li><strong>Padding:<\/strong>\u00a0set the padding from the all side\u2019s of the Progress bar<strong>.<\/strong><\/li>\n<\/ul>\n<p>Below we set the 20dp padding from all the side\u2019s of the Progress bar.<\/p>\n<pre>&lt;ProgressBar\r\n    android:id=\"@+id\/simpleProgressBar\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:max=\"100\"\r\n    android:progress=\"50\"\r\n    android:background=\"#000\"\r\n    style=\"@style\/Widget.AppCompat.ProgressBar.Horizontal\"\r\n    android:padding=\"20dp\"\/&gt;&lt;!--\/\/ 20dp padding from all the sides of the progress bar--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1022\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/padding-in-ProgressBar-Android.jpg\" alt=\"padding in ProgressBar Android\" width=\"230\" height=\"211\" \/><\/center><\/p>\n<hr \/>\n<h4><strong>ProgressBar Example In Android Studio:<\/strong><\/h4>\n<p>In the first example of ProgressBar\u00a0we displayed a default spinning wheel progress bar and a start button whenever a user click on the button the progress bar is displayed. Below is the final output, download code and step by step explanation:<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/ProgressBarExample\" target=\"_blank\" rel=\"nofollow\">Download Code<\/a><a class=\"help\" title=\"Learn How To Download Code And Import In Android Studio\" href=\"\/androidstudio\/download-code-abhiandroid\" target=\"_blank\" rel=\"nofollow\"> ? <\/a><\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1056\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/ProgressBar-Example-in-Android-Studio.jpg\" alt=\"ProgressBar Example in Android Studio\" width=\"211\" height=\"354\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/ProgressBar-Example-in-Android-Studio.jpg 211w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/ProgressBar-Example-in-Android-Studio-179x300.jpg 179w\" sizes=\"auto, (max-width: 211px) 100vw, 211px\" \/><\/center><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> Create a new project and name it ProgressBarExample.<\/p>\n<pre>Select File -&gt; New -&gt; New Project\u2026 then Fill the forms and click \"Finish\" button.<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Open res \u00a0-&gt; layout -&gt;\u00a0<strong>activity_main.xml (or) main.xml\u00a0<\/strong>and add following code:<\/p>\n<p>In this step we open an xml file and add the code for displaying a progress bar \u00a0and set its visibility to invisible\u00a0and one start button.<\/p>\n<pre>&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\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=\".MainActivity\"&gt;\r\n\r\n    &lt;ProgressBar\r\n        android:id=\"@+id\/simpleProgressBar\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:visibility=\"invisible\"\r\n        android:layout_centerHorizontal=\"true\"\/&gt;\r\n\r\n    &lt;Button\r\n        android:id=\"@+id\/startButton\"\r\n        android:layout_width=\"200dp\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:text=\"Start\"\r\n        android:textSize=\"20sp\"\r\n        android:textStyle=\"bold\"\r\n        android:layout_centerHorizontal=\"true\"\r\n        android:layout_marginTop=\"100dp\"\r\n        android:padding=\"10dp\"\r\n        android:background=\"#0f0\"\r\n        android:textColor=\"#fff\"\/&gt;\r\n\r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span> Now Open src -&gt; package -&gt;\u00a0<strong>MainActivity.java<\/strong><\/p>\n<p>In this step we open\u00a0MainActivity\u00a0where\u00a0we add the code to\u00a0initiate the progress bar &amp;\u00a0button and then perform click event on button which display the progress bar.<\/p>\n<pre>package example.gb.progressbarexample;\r\n\r\nimport android.graphics.Color;\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.os.Bundle;\r\nimport android.view.Menu;\r\nimport android.view.MenuItem;\r\nimport android.view.View;\r\nimport android.widget.ProgressBar;\r\nimport android.widget.Button;\r\n\r\npublic class MainActivity extends AppCompatActivity {\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        \/\/ initiate progress bar and start button\r\n        final ProgressBar simpleProgressBar = (ProgressBar) findViewById(R.id.simpleProgressBar);\r\n        Button startButton = (Button) findViewById(R.id.startButton);\r\n        \/\/ perform click event on button\r\n        startButton.setOnClickListener(new View.OnClickListener() {\r\n            @Override\r\n            public void onClick(View v) {\r\n                \/\/ visible the progress bar\r\n                simpleProgressBar.setVisibility(View.VISIBLE);\r\n            }\r\n        });\r\n    }\r\n\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<p>Now start the AVD in Emulator and run the App. Click on the start\u00a0button and Progress Bar will be displayed on screen.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-1056\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/ProgressBar-Example-in-Android-Studio-179x300.jpg\" alt=\"ProgressBar Example Output\" width=\"179\" height=\"300\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/ProgressBar-Example-in-Android-Studio-179x300.jpg 179w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/ProgressBar-Example-in-Android-Studio.jpg 211w\" sizes=\"auto, (max-width: 179px) 100vw, 179px\" \/><\/center><\/p>\n<hr \/>\n<h4><strong>Horizontal ProgressBar Example In Android Studio:<\/strong><\/h4>\n<p>In the second example\u00a0we display a horizontal progress bar with drawable background and a start button. Here whenever a user clicks on button a thread is used to start the progress. Below is the final output, download code and step by step explanation:<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/ProgressBarExample\" target=\"_blank\" rel=\"nofollow\">Download Code<\/a><a class=\"help\" title=\"Learn How To Download Code And Import In Android Studio\" href=\"\/androidstudio\/download-code-abhiandroid\" target=\"_blank\" rel=\"nofollow\"> ? <\/a><\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-1057\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/Horizontal-ProgressBar-Example-In-Android-Studio-172x300.jpg\" alt=\"Horizontal ProgressBar Example In Android Studio\" width=\"172\" height=\"300\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Horizontal-ProgressBar-Example-In-Android-Studio-172x300.jpg 172w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Horizontal-ProgressBar-Example-In-Android-Studio.jpg 232w\" sizes=\"auto, (max-width: 172px) 100vw, 172px\" \/><\/center><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> Create a new project and name it <strong>ProgressBarExample<\/strong>.<\/p>\n<pre>Select File -&gt; New -&gt; New Project\u2026 then Fill the forms and click \"Finish\" button.<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Open res -&gt; layout -&gt;\u00a0<strong>activity_main.xml (or) main.xml\u00a0<\/strong>and add following code:<\/p>\n<p>In this step we open an xml file and add the code for displaying a horizontal progress bar by using\u00a0style property, drawable progress xml and one start button.<\/p>\n<pre>&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:background=\"#000\"\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=\".MainActivity\"&gt;\r\n\r\n    &lt;ProgressBar\r\n        android:id=\"@+id\/simpleProgressBar\"\r\n        style=\"@style\/Widget.AppCompat.ProgressBar.Horizontal\"\r\n        android:layout_width=\"fill_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_centerHorizontal=\"true\"\r\n        android:layout_marginTop=\"70dp\"\r\n        android:max=\"100\"\r\n        android:progress=\"0\"\r\n        android:progressDrawable=\"@drawable\/custom_progress\" \/&gt;\r\n\r\n    &lt;Button\r\n        android:id=\"@+id\/startButton\"\r\n        android:layout_width=\"200dp\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_centerHorizontal=\"true\"\r\n        android:layout_marginTop=\"120dp\"\r\n        android:background=\"#0f0\"\r\n        android:padding=\"10dp\"\r\n        android:text=\"Start\"\r\n        android:textColor=\"#fff\"\r\n        android:textSize=\"20sp\"\r\n        android:textStyle=\"bold\" \/&gt;\r\n\r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span>\u00a0 Create an xml file \u00a0in drawable -&gt; custom_progress.xml<\/p>\n<p>In this step we create a custom drawable xml for the progress bar. In this xml we create a\u00a0layer list in which we create an item and then set the gradient colors for our custom progress bar.<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;layer-list xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\" &gt;\r\n\r\n    &lt;item&gt;\r\n        &lt;shape&gt;\r\n            &lt;gradient\r\n                android:endColor=\"#fff\"\r\n                android:startColor=\"#1f1\"\r\n                android:useLevel=\"true\" \/&gt;\r\n\r\n        &lt;\/shape&gt;\r\n    &lt;\/item&gt;\r\n\r\n\r\n&lt;\/layer-list&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 4:<\/strong><\/span> Open \u00a0app\u00a0-&gt; package -&gt;\u00a0<strong>MainActivity.java<\/strong><\/p>\n<p>In this step we open\u00a0MainActivity and add the code to\u00a0initiate the progress bar, button and then perform click event on button. After that\u00a0we\u00a0start\u00a0the progressing in a progress bar using a thread.<\/p>\n<pre>package example.gb.progressbarexample;\r\n\r\nimport android.app.Dialog;\r\nimport android.content.DialogInterface;\r\nimport android.content.Intent;\r\nimport android.graphics.Color;\r\nimport android.provider.Settings;\r\nimport android.support.v7.app.AlertDialog;\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.os.Bundle;\r\nimport android.view.Menu;\r\nimport android.view.MenuItem;\r\nimport android.view.View;\r\nimport android.widget.ProgressBar;\r\nimport android.widget.Button;\r\nimport android.widget.Toast;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n    int progress = 0;\r\n    ProgressBar simpleProgressBar;\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        \/\/ initiate progress bar and start button\r\n        simpleProgressBar = (ProgressBar) findViewById(R.id.simpleProgressBar);\r\n        Button startButton = (Button) findViewById(R.id.startButton);\r\n        \/\/ perform click event on button\r\n        startButton.setOnClickListener(new View.OnClickListener() {\r\n            @Override\r\n            public void onClick(View v) {\r\n                \/\/ call a function\r\n                setProgressValue(progress);\r\n\r\n            }\r\n        });\r\n    }\r\n\r\n    private void setProgressValue(final int progress) {\r\n\r\n        \/\/ set the progress\r\n        simpleProgressBar.setProgress(progress);\r\n        \/\/ thread is used to change the progress value\r\n        Thread thread = new Thread(new Runnable() {\r\n            @Override\r\n            public void run() {\r\n                try {\r\n                    Thread.sleep(1000);\r\n                } catch (InterruptedException e) {\r\n                    e.printStackTrace();\r\n                }\r\n                setProgressValue(progress + 10);\r\n            }\r\n        });\r\n        thread.start();\r\n    }\r\n\r\n\r\n    @Override\r\n    public boolean onCreateOptionsMenu(Menu menu) {\r\n        \/\/ Inflate the menu; this adds items to the action bar if it is present.\r\n        getMenuInflater().inflate(R.menu.menu_main, menu);\r\n        return true;\r\n    }\r\n\r\n    @Override\r\n    public boolean onOptionsItemSelected(MenuItem item) {\r\n        \/\/ Handle action bar item clicks here. The action bar will\r\n        \/\/ automatically handle clicks on the Home\/Up button, so long\r\n        \/\/ as you specify a parent activity in AndroidManifest.xml.\r\n        int id = item.getItemId();\r\n\r\n        \/\/noinspection SimplifiableIfStatement\r\n        if (id == R.id.action_settings) {\r\n            return true;\r\n        }\r\n\r\n        return super.onOptionsItemSelected(item);\r\n    }\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<p>Now run the App in AVD, click on start button and you will see horizontal progressbar.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-1057\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/Horizontal-ProgressBar-Example-In-Android-Studio-172x300.jpg\" alt=\"Horizontal ProgressBar Example In Android Studio\" width=\"172\" height=\"300\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Horizontal-ProgressBar-Example-In-Android-Studio-172x300.jpg 172w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Horizontal-ProgressBar-Example-In-Android-Studio.jpg 232w\" sizes=\"auto, (max-width: 172px) 100vw, 172px\" \/><\/center><\/p>\n<hr \/>\n<h4><strong>Related\u00a0Topic &#8211; ProgressDialog In Android<\/strong><\/h4>\n<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. Read our <a href=\"\/ui\/progressdialog\">Progressdialog tutorial with example<\/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\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/progressbar\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">ProgressBar 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-988","page","type-page","status-publish","hentry"],"acf":[],"psp_head":"<title>ProgressBar Tutorial With Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Learn progressbar methods and attribute with example in Android Studio. ProgressBar is used to display the status of work being done like analyzing status of work or downloading a file etc.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/progressbar\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/988","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=988"}],"version-history":[{"count":3,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/988\/revisions"}],"predecessor-version":[{"id":2793,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/988\/revisions\/2793"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}