Progress bar for android Development

Source: Internet
Author: User

Progress bar for android Development
1. Normal progress bar
<ProgressBar
Android: id = "@ + id/firstBar"
// Set the progress bar style to horizontal
Style = "? Android: attr/progressBarStyleHorizontal"
Android: layout_width = "200dp"
Android: layout_height = "wrap_content"
// Set the progress bar to invisible
Android: visibility = "gone"/>
2. Circular progress bar
<ProgressBar
Android: id = "@ + id/secondBar"
// Set the progress bar style to circle
Style = "? Android: attr/progressBarStyle"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: visibility = "gone"/>


 
 
  1. Package com. example. progressbar;

  2. Import android. app. Activity;
  3. Import android. OS. Bundle;
  4. Import android. view. View;
  5. Import android. view. View. OnClickListener;
  6. Import android. widget. Button;
  7. Import android. widget. ProgressBar;


  8. Public class MainActivity extends Activity
  9. {
  10. Private ProgressBar firstbar = null;
  11. Private ProgressBar secondbar = null;
  12. Private Button pbtn = null;
  13. Private int I = 0;
  14. @ Override
  15. Protected void onCreate (Bundle savedInstanceState)
  16. {
  17. // TODO Auto-generated method stub
  18. Super. onCreate (savedInstanceState );
  19. SetContentView (R. layout. activity_main );

  20. Firstbar = (ProgressBar) findViewById (R. id. firstBar );
  21. Secondbar = (ProgressBar) findViewById (R. id. secondBar );
  22. Pbtn = (Button) findViewById (R. id. pbtn );
  23. Pbtn. setOnClickListener (new OnClickListener ()
  24. {
  25. @ Override
  26. Public void onClick (View v)
  27. {
  28. // TODO Auto-generated method stub
  29. If (I = 0)
  30. {
  31. Firstbar. setVisibility (View. VISIBLE); // set the first progress bar to VISIBLE
  32. Secondbar. setVisibility (View. VISIBLE); // set the second progress bar to VISIBLE
  33. }
  34. Else if (I <100)
  35. {
  36. // There are two progresses in the first progress bar
  37. Firstbar. setProgress (I); // set the progress bar
  38. Firstbar. setSecondaryProgress (I + 10); // set the progress bar
  39. Secondbar. setProgress (I );
  40. }
  41. Else if (I >= 100)
  42. {
  43. I = 0;
  44. }
  45. Else
  46. {
  47. Firstbar. setVisibility (View. GONE); // sets the progress bar to invisible.
  48. Secondbar. setVisibility (View. GONE); // sets the progress bar to invisible.
  49. }
  50. I = I + 10;
  51. }
  52. });
  53. }

  54. }
3. Progress bar with slider seekBar
How to Use seekBar:
1. Declare SeekBar in the layout File
<SeekBar
Android: id = "@ + id/seekBar"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
2. Define an OnSeekBarChangeListener, that is, set the listener for SeekBar.
Class SeekBarListener implements OnSeekBarChangeListener {
@ Override
// Call this function when the progress bar changes. Note that the changes here include the automatic change of the progress bar or the slider of the progress bar dragged by hand.
Public void onProgressChanged (SeekBar seekBar, int progress,
Boolean fromUser)
{
// TODO Auto-generated method stub

}
@ Override
// This function is called when the user starts to drag the slider.
Public void onStartTrackingTouch (SeekBar seekBar)
{
// TODO Auto-generated method stub

}
@ Override
// This function is called when the user stops dragging the slider.
Public void onStopTrackingTouch (SeekBar seekBar)
{
// TODO Auto-generated method stub

}
}

Source code
 
 
  1. Public class MainActivity extends Activity
  2. {
  3. Private SeekBar seekBar = null;

  4. @ Override
  5. Protected void onCreate (Bundle savedInstanceState)
  6. {
  7. Super. onCreate (savedInstanceState );
  8. SetContentView (R. layout. activity_main );

  9. SeekBar = (SeekBar) findViewById (R. id. seekBar );
  10. SeekBar. setOnSeekBarChangeListener (new SeekBarListener ());
  11. }

  12. Class SeekBarListener implements OnSeekBarChangeListener {

  13. @ Override
  14. Public void onProgressChanged (SeekBar seekBar, int progress,
  15. Boolean fromUser)
  16. {
  17. // TODO Auto-generated method stub
  18. // Print the position of the current progress bar
  19. System. out. println (progress );
  20. }

  21. @ Override
  22. Public void onStartTrackingTouch (SeekBar seekBar)
  23. {
  24. // TODO Auto-generated method stub
  25. // The Position of the progress bar when the user starts to drag the slider
  26. System. out. println ("start -->" + seekBar. getProgress ());
  27. }

  28. @ Override
  29. Public void onStopTrackingTouch (SeekBar seekBar)
  30. {
  31. // TODO Auto-generated method stub
  32. // The Position of the progress bar when the user stops dragging the slider
  33. System. out. println ("end -->" + seekBar. getProgress ());
  34. }
  35. }

  36. @ Override
  37. Public boolean onCreateOptionsMenu (Menu menu)
  38. {
  39. // Inflate the menu; this adds items to the action bar if it is present.
  40. GetMenuInflater (). inflate (R. menu. main, menu );
  41. Return true;
  42. }

  43. }
Layout File
 
 
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity" >

  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="@string/hello_world" />
  14. <SeekBar
  15. android:id="@+id/seekBar"
  16. android:layout_width="fill_parent"
  17. android:layout_height="wrap_content"
  18. />

  19. </RelativeLayout>
4. Star progress bar RatingBar
/* Usage of RatingBar
* 1. Declare a RatingBar in the layout file.
* <RatingBar
Android: id = "@ + id/ratingBar"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
// Set the number of stars in ratingBar
Android: numStars = "5"
// Set the size of each drag to 1 star
Android: stepSize = "1.0"
/>
2. Define an OnRatingBarListener and set a listener for ratingBar.
Class RatingBarListener implements RatingBar. OnRatingBarChangeListener {
@ Override
// This function is called when you drag ratingBar.
Public void onRatingChanged (RatingBar ratingBar, float rating,
Boolean fromUser)
{
// TODO Auto-generated method stub


}
}
*/


Source code
 
 
  1. Public class MainActivity extends Activity
  2. {
  3. Private RatingBar ratingBar = null;
  4. @ Override
  5. Protected void onCreate (Bundle savedInstanceState)
  6. {
  7. Super. onCreate (savedInstanceState );
  8. SetContentView (R. layout. activity_main );
  9. // Find the ratingBar
  10. RatingBar = (RatingBar) findViewById (R. id. ratingBar );
  11. // Bind the listener
  12. RatingBar. setOnRatingBarChangeListener (new RatingBarListener ());
  13. }

  14. Class RatingBarListener implements RatingBar. OnRatingBarChangeListener {
  15. @ Override
  16. // This function is called when you drag ratingBar.
  17. Public void onRatingChanged (RatingBar ratingBar, float rating,
  18. Boolean fromUser)
  19. {
  20. // TODO Auto-generated method stub
  21. System. out. println ("ratingBar" + rating );
  22. }
  23. }

  24. @ Override
  25. Public boolean onCreateOptionsMenu (Menu menu)
  26. {
  27. // Inflate the menu; this adds items to the action bar if it is present.
  28. GetMenuInflater (). inflate (R. menu. main, menu );
  29. Return true;
  30. }

  31. }
Layout File
 
 
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity" >

  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="@string/hello_world" />

  14. <RatingBar
  15. android:id="@+id/ratingBar"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:numStars="5"
  19. android:stepSize="1.0"
  20. />

  21. </RelativeLayout>







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.