ProgressBar when Android is used to display the progress of the component. When performing a more time-consuming operation (such as IO operations, network operations, etc.), to avoid the user experience being reduced by the lack of changes to the interface, a progress bar is provided to let the user know that the program is still running.
First, ProgressBar has the following several common styles
1. The default progress bar style is circled (for medium-sized circles)
<progressbar
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
/>
Note that when the style is a circle, its progress is constantly spinning, just to tell the user that the program is executing, and that it is not accurate to tell the users the exact progress.
And the default display is rotated, and it can't be stopped until it is hidden by the program (such as when task processing is complete).
If you want to display as a circle, but also control its pause and rotation, need to use a custom way, this is not introduced here.
Hide the progress bar, very simple: xxxx.setvisibility (view.gone);
2. A circle showing large size
<progressbar
Style= "? Android:attr/progressbarstylelarge"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
/>
3. A circle showing small size
<progressbar
Style= "? Android:attr/progressbarstylesmall"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
/>
You can set layout_width and layout_height directly to a specific value to control the radius of the circle.
4, displayed as a straight bar
<progressbar
Style= "? Android:attr/progressbarstylehorizontal"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
/>
For a straight bar style, you can not only make it look like a circle, the default is that the progress is constantly changing, and you can precisely control its progress, which is highlighted below.
Second, straight bar progress bar
Set to a style that has been rotated
<progressbar
Android:id= "@+id/progress1"
Style= "? Android:attr/progressbarstylehorizontal"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:indeterminate= "true"
/>
Setting the Indeterminate property to True can be displayed when the progress bar is constantly changing, and the property defaults to False.
If you want to make it stop changing, you can control it by code:
ProgressBar Pro1 = (ProgressBar) Findviewbyid (R.ID.PROGRESS1);
Pro1.setindeterminate (FALSE);
If you want it to stop, the progress display is complete, and you can code like this:
Pro1.setprogress (100);
Note The default maximum size of the progress bar is 100, and you can set the value you want by Setmax.
Iii. precise control of progress (combined with asynctask)
The code is as follows
public void startprogress (view view) {final ProgressBar Pro1 = (ProgressBar) Findviewbyid (R.ID.PROGRESS1); new Asynctask<integer, Integer, integer> () {@Overrideprotected integer Doinbackground (Integer ... params) {for (int i = 1; i <=; i++) dosomethingtask (i); return 100;} private void dosomethingtask (int num) {try {thread.sleep ();p ublishprogress (num);} catch (Interruptedexception e) { E.printstacktrace ();}} @Overrideprotected void OnPostExecute (Integer result) {TextView Resultview = (TextView) Findviewbyid (R.id.resulttext); Resultview.setvisibility (view.visible); Resultview.settext ("The result is" + result); Super.onpostexecute (result); @Overrideprotected void Onprogressupdate (Integer ... values) {pro1.setprogress (values[0]); Super.onprogressupdate ( values);}}. Execute (1);}
Perform background tasks in the Doinbackground method of Asynctask (actually executing in another thread, such as IO operations, network operations. The interface component cannot be manipulated in this method, and if you want the progress bar to show the progress in the middle, you can call the Publishprogress method to send the progress during the execution of the background task. The execution of the Publishprogress method triggers the Onprogressupdate method, which is done in the main thread, where the interface component can be updated to update the progress bar. When the Doinbackground method is executed, the OnPostExecute method is triggered (which is done in the main thread, the interface component can be updated in the method), and the task completion interface can be displayed in this method, which is shown in the view below.
With the code and examples above, you can basically meet most scenarios. Finally, ProgressBar's progress can only be controlled by code and cannot be dragged by the user. If you want to allow users to drag progress, such as in a video or audio playback interface, you can use the Seekbar component.
Android Learning Note: The use of progress bar ProgressBar and its use in conjunction with Asynctask