<?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" >
<ProgressBar android:id="@+id/progressBar1" style="?
android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="false"
android:max="10"
android:padding="4dip" >
</ProgressBar>
<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="startProgress"
android:text="Start Progress" >
</Button>
</LinearLayout>
Now make an java file and insert the following code into it.
private Handler handler;
private ProgressBar progress;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
progress = (ProgressBar) findViewById(R.id.progressBar1);
handler = new Handler();
}
public void startProgress(View view) {
// Do something long
Runnable runnable = new Runnable() {
@Override public void run()
{
for (int i = 0; i <= 10; i++) {
final int value = i;
try {
Thread.sleep(2000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
@Override public void run() {
progress.setProgress(value);
}
});
}
}
};
new Thread(runnable).start();
}
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.