Android ProgressBar - Code Snippets

Visual indicator of progress in some operation. Displays a bar to the user representing how far the operation has progressed; the application can change the amount of progress (modifying the length of the bar) as it moves forward. There is also a secondary progress displayable on a progress bar which is useful for displaying intermediate progress, such as the buffer level during a streaming playback progress bar.A progress bar can also be made indeterminate. In indeterminate mode, the progress bar shows a cyclic animation without an indication of progress. This mode is used by applications when the length of the task is unknown. The indeterminate progress bar can be either a spinning wheel or a horizontal bar.

XML Snippet
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Small"
android:layout_marginRight="5dp"/>

Java Snippet
 public class MyActivity extends Activity
{
private static final int PROGRESS = 0x1;
private ProgressBar mProgress;
private int mProgressStatus = 0;
private Handler mHandler = new Handler();
protected void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.progressbar_activity);
mProgress = (ProgressBar) findViewById(R.id.progress_bar);
// Start lengthy operation in a background thread
new Thread(new Runnable()
{
public void run()
{
while (mProgressStatus< 100)
{
mProgressStatus = doWork();
// Update the progress bar
mHandler.post(new Runnable()
{
public void run()
{
mProgress.setProgress(mProgressStatus);
}
});
}
}
}).start();
}
}

Finally,
If you want us to write any tutorial, tell me in the comments below!
Comment via Facebook
0 Comment via Google

0 تعليقات:

Post a Comment