Android control's ProgressBar

Source: Internet
Author: User

The ProgressBar is located under the android. widget package, which inherits from the View and is mainly used to display the progress of some operations. The length of an application can be modified to indicate the completion of the current background operation. The progress bar will move, so when loading some resources for a long time or performing some time-consuming operations, the user interface will not lose response. The ProgressBar class is easy to use. You only need to display it to the foreground, and then start a background thread to regularly change the value indicating the progress.

The following ProgressBar is used with Handle to simulate the use of the progress bar. When the progress bar is complete, it will jump to TestActivity.

Main. xml layout File

<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<! -- Rectangular progress bar, which is invisible at the beginning until you click the button -->
<ProgressBar android: id = "@ + id/progressBar"
Style = "? Android: attr/progressBarStyleHorizontal"
Mce_style = "? Android: attr/progressBarStyleHorizontal"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: visibility = "gone"
Android: max = "100" type = "codeph" text = "/codeph"/>
<! -- Circular progress bar -->
<! -- <ProgressBar android: id = "@ + id/progressBar"
Style = "? Android: attr/progressBarStyleLarge"
Mce_style = "? Android: attr/progressBarStyleLarge"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/> -->
<Button android: id = "@ + id/start"
Android: text = "progress bar"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
<Button android: id = "@ + id/stop"
Android: text = "stop progress bar"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
</LinearLayout>

PbActivity

Package com. ljq. pb;

Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. view. View;
Import android. widget. Button;
Import android. widget. ProgressBar;

Public class PbActivity extends Activity {
Private ProgressBar progressBar = null;
Private Button start = null, stop = null;
// Define the Handler object
Private Handler handler = new Handler ();


@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

ProgressBar = (ProgressBar) findViewById (R. id. progressBar );
ProgressBar. setProgress (0 );

Start = (Button) findViewById (R. id. start );
Start. setOnClickListener (new View. OnClickListener (){

Public void onClick (View v ){
Handler. post (runnable); // start execution
}

});
Stop = (Button) findViewById (R. id. stop );
Stop. setOnClickListener (new View. OnClickListener (){

Public void onClick (View v ){
Handler. removeCallbacks (runnable); // stop the execution
ProgressBar. setProgress (0 );
}

});
}

Int pro = 0;
Runnable runnable = new Runnable (){
Public void run (){
ProgressBar. setVisibility (View. VISIBLE );
Pro = progressBar. getProgress () + 10;
ProgressBar. setProgress (pro );
// If the progress is less than 100, the runnable will be executed again after 1000 milliseconds.
If (pro <100 ){
Handler. postDelayed (runnable, 1000 );
} Else {
ProgressBar. setVisibility (View. GONE );
StartActivity (new Intent (PbActivity. this, TestActivity. class ));
Handler. removeCallbacks (runnable );
ProgressBar. setProgress (0 );
}
}
};
}

Running result

  

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.