In Android, progress bar is useful to tell user that the task is takes longer time to finish.
In this tutorial, we show you how to display a progress bar dialog to tell user that your task is running, and also how to increase the progress bar status until the task is completed.
File : res/layout/main.xml
File : MyAndroidAppActivity.java
P.S The “doSomeTasks” method is just a file size download simulator, just replace this method with your long running task.
1. Result, a single button.
2. Click on the button, it will prompt a “progress bar dialog” to show the current download progress.
3. Task is completed, progress bar will show 100%, and close automatically.
In this tutorial, we show you how to display a progress bar dialog to tell user that your task is running, and also how to increase the progress bar status until the task is completed.
Note
Refer to this Android ProgressBar JavaDoc for detail explanation.
P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3.Refer to this Android ProgressBar JavaDoc for detail explanation.
1. Add a Button
Open “res/layout/main.xml” file, just add normal button for demonstration.File : res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/btnStartProgress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Download File" /> </LinearLayout>
2. Code Code
The key to use progress bar is using “Thread
” to run your time consume task and another “Thread
” to update the progress bar status accordingly. Read the code’s comment, it should be self-explanatory.File : MyAndroidAppActivity.java
package com.mkyong.android; import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle; import android.os.Handler; import android.widget.Button; import android.view.View; import android.view.View.OnClickListener; public class MyAndroidAppActivity extends Activity { Button btnStartProgress; ProgressDialog progressBar; private int progressBarStatus = 0; private Handler progressBarHandler = new Handler(); private long fileSize = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); addListenerOnButton(); } public void addListenerOnButton() { btnStartProgress = (Button) findViewById(R.id.btnStartProgress); btnStartProgress.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { // prepare for a progress bar dialog progressBar = new ProgressDialog(v.getContext()); progressBar.setCancelable(true); progressBar.setMessage("File downloading ..."); progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressBar.setProgress(0); progressBar.setMax(100); progressBar.show(); //reset progress bar status progressBarStatus = 0; //reset filesize fileSize = 0; new Thread(new Runnable() { public void run() { while (progressBarStatus < 100) { // process some tasks progressBarStatus = doSomeTasks(); // your computer is too fast, sleep 1 second try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } // Update the progress bar progressBarHandler.post(new Runnable() { public void run() { progressBar.setProgress(progressBarStatus); } }); } // ok, file is downloaded, if (progressBarStatus >= 100) { // sleep 2 seconds, so that you can see the 100% try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } // close the progress bar dialog progressBar.dismiss(); } } }).start(); } }); } // file download simulator... a really simple public int doSomeTasks() { while (fileSize <= 1000000) { fileSize++; if (fileSize == 100000) { return 10; } else if (fileSize == 200000) { return 20; } else if (fileSize == 300000) { return 30; } // ...add your own } return 100; } }
3. Demo
Run the application.1. Result, a single button.
Download Source Code
Download it – Android-ProgressBar-Example.zip (15 KB)
chenenen0506
ReplyDeletetory burch boots
jordan retro 11
nike trainers uk
air force 1
cheap toms
louis vuitton outlet
kate spade outlet
louis vuitton outlet stores
true religion jeans
celine handbags
nike store
air max 90
jordan retro 8
coach outlet
louis vuitton purses
cheap jordans
hollister uk
tory burch outlet online
pandora outlet
fitflops shoes
michael kors outlet
jordan 11
discount jordans
louis vuitton outlet
hollister
christian louboutin shoes
kobe 10
gucci handbags
christian louboutin sale
fit flops
ReplyDeletehttps://khalejmovers.com/نقل-اثاث-بالاحساء/
https://khalejmovers.com/نقل-اثاث-بجدة/
https://khalejmovers.com/نقل-اثاث-من-الرياض-الى-القصيم/
https://khalejmovers.com/شركة-نقل-اثاث-من-الرياض-الى-البحرين/
https://khalejmovers.com/نقل-اثاث-من-الرياض-الى-الدمام/