Android UI ProgressBar (progress bar)

Source: Internet
Author: User

A progress bar is a very practical component used to display the progress percentage of a user's time-consuming operations. First, let's take a look at the progress bars of several styles supported by Android:

Style = "@ android: style/Widget. ProgressBar. Inverse" normal size progress bar


Style = "@ android: style/Widget. ProgressBar. Large" Large progress bar


Style = "@ android: style/Widget. ProgressBar. Large. Inverse" Large progress bar


Style = "@ android: style/Widget. ProgressBar. Small" Small progress bar
Style = "@ android: style/Widget. ProgressBar. Small. Inverse" Small progress bar


Style = "@ android: style/Widget. ProgressBar. Horizontal" Horizontal progress bar


Add the progress bars of these six different styles in the style file to see the effects on the simulator.

 

<? 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">
 
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "progress bar Demo"/>
 
<ProgressBar
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: max = "1000"
Android: progress = "100"
Android: id = "@ + id/progressbar1"
/>

<ProgressBar
Style = "@ android: style/Widget. ProgressBar. Inverse"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: max = "100"
Android: progress = "20"
/>
<ProgressBar
Style = "@ android: style/Widget. ProgressBar. Large"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: max = "100"
Android: progress = "20"
/>
<ProgressBar
Style = "@ android: style/Widget. ProgressBar. Large. Inverse"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: max = "100"
Android: progress = "20"
/>
<ProgressBar
Style = "@ android: style/Widget. ProgressBar. Small"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: max = "100"
Android: progress = "20"
/>
<ProgressBar
Style = "@ android: style/Widget. ProgressBar. Small. Inverse"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: max = "100"
Android: progress = "20"
/>
<ProgressBar
Style = "@ android: style/Widget. ProgressBar. Horizontal"
Android: layout_marginTop = "30dp"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: max = "1000"
Android: progress = "500"
Android: secondaryProgress = "300"
Android: id = "@ + id/progressbar2"
/>
</LinearLayout>
<? 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">

<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "progress bar Demo"/>

<ProgressBar
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: max = "1000"
Android: progress = "100"
Android: id = "@ + id/progressbar1"
/>

<ProgressBar
Style = "@ android: style/Widget. ProgressBar. Inverse"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: max = "100"
Android: progress = "20"
/>
<ProgressBar
Style = "@ android: style/Widget. ProgressBar. Large"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: max = "100"
Android: progress = "20"
/>
<ProgressBar
Style = "@ android: style/Widget. ProgressBar. Large. Inverse"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: max = "100"
Android: progress = "20"
/>
<ProgressBar
Style = "@ android: style/Widget. ProgressBar. Small"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: max = "100"
Android: progress = "20"
/>
<ProgressBar
Style = "@ android: style/Widget. ProgressBar. Small. Inverse"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: max = "100"
Android: progress = "20"
/>
<ProgressBar
Style = "@ android: style/Widget. ProgressBar. Horizontal"
Android: layout_marginTop = "30dp"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: max = "1000"
Android: progress = "500"
Android: secondaryProgress = "300"
Android: id = "@ + id/progressbar2"
/>
</LinearLayout> effects on simulators:

 

There is no declared style for the first progress bar from top to bottom. It is a normal size progress bar like the second, and a large progress bar for the third and fourth by default, the fifth, sixth, is a small progress bar, and the last is a horizontal progress bar.

In addition to the following common attributes of style ProgressBar:

Android: max sets the maximum value of the progress bar (the last horizontal progress bar is set to 1000)

Android: progress sets the current progress (the last horizontal progress bar sets the current progress to 500)

Android: secondaryProgress sets the second progress bar

 


Simulate the progress change of a progress bar:

Package cn. class3g. activity;
 
Import android. app. Activity;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. util. Log;
Import android. widget. ProgressBar;
 
Public class ProgressBarDemo extends Activity {

ProgressBar progressbar = null;
Static int I = 0;
Int progressbarMax = 0;
 
Handler handler = new Handler ();

Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. progressbar_layout );

FindViews ();
}
 
Private void findViews (){
Progressbar = (ProgressBar) this. findViewById (R. id. progressbar2 );
Progressbar. setMax (1000 );
ProgressbarprogressbarMax = progressbar. getMax ();


// Use a thread to control the progress of the progress bar
New Thread (new Runnable (){

Public void run (){
While (I <progressbarMax ){
// DoWord () simulates the progress of a task
I = doWork ();

Handler. post (new Runnable (){
Public void run (){
Progressbar. setProgress (I );
}
});
Try {
Thread. sleep (50 );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}
}). Start ();
 
}

Public int doWork (){
Log. d ("TAG", String. valueOf (I ));
Return ++ I;
}
}
Package cn. class3g. activity;

Import android. app. Activity;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. util. Log;
Import android. widget. ProgressBar;

Public class ProgressBarDemo extends Activity {

ProgressBar progressbar = null;
Static int I = 0;
Int progressbarMax = 0;

Handler handler = new Handler ();
 
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. progressbar_layout );

FindViews ();
}

Private void findViews (){
Progressbar = (ProgressBar) this. findViewById (R. id. progressbar2 );
Progressbar. setMax (1000 );
ProgressbarMax = progressbar. getMax ();


// Use a thread to control the progress of the progress bar
New Thread (new Runnable (){

Public void run (){
While (I <progressbarMax ){
// DoWord () simulates the progress of a task
I = doWork ();

Handler. post (new Runnable (){
Public void run (){
Progressbar. setProgress (I );
}
});
Try {
Thread. sleep (50 );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}
}). Start ();

}
 
Public int doWork (){
Log. d ("TAG", String. valueOf (I ));
Return ++ I;
}
} Results:

 

Author: jjaze3344
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.