Android loading progress bar

Source: Internet
Author: User

Package xiaohang. zhimeng;

Import android. app. Activity;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. OS. Message;
Import android. view. View;
Import android. view. Window;
Import android. widget. Button;
Import android. widget. ProgressBar;

Public class Activity01 extends Activity {

// Declare the ProgressBar object
Private ProgressBar xh_ProgressBar;
Private ProgressBar xh_ProgressBar2;
Private Button xh_Button;
Protected static final int GUI_STOP_NOTIFIER = 0x108;
Protected static final int GUI_THREADING_NOTIFIER = 0x109;
Public int intCounter = 0;

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// Set the window mode because the progress bar needs to be displayed in the title bar
// RequestWindowFeature (Window. FEATURE_PROGRESS );
// SetProgressBarVisibility (true );
SetContentView (R. layout. main );

// Obtain the ssssbar
Xh_ProgressBar = (ProgressBar) findViewById (R. id. ProgressBar01 );
Xh_ProgressBar2 = (ProgressBar) findViewById (R. id. ProgressBar02 );
Xh_Button = (Button) findViewById (R. id. Button01 );

// Set the progress bar to the uncertain Mode
Xh_ProgressBar.setIndeterminate (false );
Xh_ProgressBar2.setIndeterminate (false );

// Start execution when the button is pressed
Xh_Button.setOnClickListener (new Button. OnClickListener (){
@ Override
Public void onClick (View v ){
// Set ProgressBar to visible
Xh_ProgressBar.setVisibility (View. VISIBLE );
Xh_ProgressBar2.setVisibility (View. VISIBLE );
// Set the maximum value of ProgressBar
Xh_ProgressBar.setMax (100 );
// Set the current value of ProgressBar
Xh_ProgressBar.setProgress (0 );
Xh_ProgressBar2.setProgress (0 );

// Change the value of ProgressBar through a thread
New Thread (new Runnable (){
@ Override
Public void run (){
For (int I = 0; I <10; I ++ ){
Try {
// Set the progress Value
IntCounter = (I + 1) * 20;
// Sleep for 1000 milliseconds
Thread. sleep (1000 );

If (I = 4 ){
Message m = new Message ();

M. what = Activity01.GUI _ STOP_NOTIFIER;
Activity01.this. myMessageHandler
. SendMessage (m );
Break;
} Else {
Message m = new Message ();
M. what = Activity01.GUI _ THREADING_NOTIFIER;
Activity01.this. myMessageHandler
. SendMessage (m );
}

} Catch (Exception e ){
E. printStackTrace ();
}
}
}

}). Start ();
}

});
}

Handler myMessageHandler = new Handler (){
@ Override
Public void handleMessage (Message msg ){
Switch (msg. what ){
// ProgressBar is already the maximum value
Case Activity01.GUI _ STOP_NOTIFIER:
Xh_ProgressBar.setVisibility (View. GONE );
Xh_ProgressBar2.setVisibility (View. GONE );
Thread. currentThread (). interrupted ();
Break;
Case Activity01.GUI _ THREADING_NOTIFIER:
If (! Thread. currentThread (). isInterrupted ()){
// Change the current value of ProgressBar
Xh_ProgressBar.setProgress (intCounter );
Xh_ProgressBar2.setProgress (intCounter );

// Set a progress bar value in the title bar.
SetProgress (intCounter * 100 );
}
Break;
}
Super. handleMessage (msg );
}
};
}

XML 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"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>

<ProgressBar
Android: id = "@ + id/ProgressBar01"
Style = "? Android: attr/progressBarStyleHorizontal"
Android: layout_width = "200dp"
Android: layout_height = "wrap_content"
Android: visibility = "gone"
/>

<ProgressBar
Android: id = "@ + id/ProgressBar02"
Style = "? Android: attr/progressBarStyleLarge"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: max = "100"
Android: progress = "50"
Android: secondaryProgress = "70"
Android: visibility = "gone"
/>

<Button
Android: id = "@ + id/Button01"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = ""/>

</LinearLayout>

Related Article

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.