Android notes -- ProgressBar (progress bar)
The progress bar can easily tell the user the progress of the current task, especially when the execution program is relatively long, there is no progress bar, the user does not know that the program is being executed, it will be forced to close the program.
The progress bar is divided into: 1. If there is a clear progress information, it will be represented by a horizontal bar with a scale
2. There is no clear progress information (for example, a graph with a circle)
3. You can customize the progress bar (you can customize the picture and rotation direction of the progress bar)
MainActivity. java
Package com. example. progressbar; import android. OS. bundle; import android. app. activity; import android. app. progressDialog; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. view. window; import android. widget. button; import android. widget. progressBar; public class MainActivity extends Activity implements OnClickListener {private Button btnIncrement, btn Decrement; private ProgressBar pb; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // set the title progress bar to requestWindowFeature (Window. FEATURE_INDETERMINATE_PROGRESS); setContentView (R. layout. activity_main); // set to visible setProgressBarIndeterminateVisibility (true); btnIncrement = (Button) findViewById (R. id. increment); btnDecrement = (Button) findViewById (R. id. decres Ment); btnIncrement. setOnClickListener (this); btnDecrement. setOnClickListener (this); pb = (ProgressBar) findViewById (R. id. progressBar4);} public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true;} // when there are many buttons, you must set the button listener. At this time, the simplest way is to make the Activity implement the OnClickListener () interface, // use setOnClickListene R registers the Activity instance to Buttonpublic void onClick (View v) {switch (v. getId () {case R. id. increment: pb. incrementProgressBy (5); break; case R. id. decrement: pb. incrementProgressBy (-5); break; default: break;} public void show (View v) {// create a progress bar dialog box component ProgressDialog pd = new ProgressDialog (this ); pd. setTitle ("dialog box"); pd. setMessage ("download... "); // Set to horizontal dialog box pd. setProgressStyle (ProgressDialog. STYLE_HORIZONTAL); pd. show ();}}Activity_main.xml
Pb_custom.xml
It mainly sets the progress bar configuration for customizing the rotation of a small robot, from 0 ~ 360 degrees of rotation. The center of rotation is the center of the image, which is set according to X and Y.
It can be seen that the first three circles are unclear progress bars, which keep turning. There is a horizontal progress bar. Then I set two buttons to adjust the progress of the horizontal progress bar. The last small robot is a custom dialog box. Because there is no suitable image, the system's default robot is selected, but the effect is the same as the principle.
The dialog box button is set here.