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"/>
- Package com. example. progressbar;
- Import android. app. Activity;
- Import android. OS. Bundle;
- Import android. view. View;
- Import android. view. View. OnClickListener;
- Import android. widget. Button;
- Import android. widget. ProgressBar;
- Public class MainActivity extends Activity
- {
- Private ProgressBar firstbar = null;
- Private ProgressBar secondbar = null;
- Private Button pbtn = null;
- Private int I = 0;
- @ Override
- Protected void onCreate (Bundle savedInstanceState)
- {
- // TODO Auto-generated method stub
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. activity_main );
- Firstbar = (ProgressBar) findViewById (R. id. firstBar );
- Secondbar = (ProgressBar) findViewById (R. id. secondBar );
- Pbtn = (Button) findViewById (R. id. pbtn );
- Pbtn. setOnClickListener (new OnClickListener ()
- {
- @ Override
- Public void onClick (View v)
- {
- // TODO Auto-generated method stub
- If (I = 0)
- {
- Firstbar. setVisibility (View. VISIBLE); // set the first progress bar to VISIBLE
- Secondbar. setVisibility (View. VISIBLE); // set the second progress bar to VISIBLE
- }
- Else if (I <100)
- {
- // There are two progresses in the first progress bar
- Firstbar. setProgress (I); // set the progress bar
- Firstbar. setSecondaryProgress (I + 10); // set the progress bar
- Secondbar. setProgress (I );
- }
- Else if (I >= 100)
- {
- I = 0;
- }
- Else
- {
- Firstbar. setVisibility (View. GONE); // sets the progress bar to invisible.
- Secondbar. setVisibility (View. GONE); // sets the progress bar to invisible.
- }
- I = I + 10;
- }
- });
- }
- }
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
- Public class MainActivity extends Activity
- {
- Private SeekBar seekBar = null;
- @ Override
- Protected void onCreate (Bundle savedInstanceState)
- {
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. activity_main );
- SeekBar = (SeekBar) findViewById (R. id. seekBar );
- SeekBar. setOnSeekBarChangeListener (new SeekBarListener ());
- }
- Class SeekBarListener implements OnSeekBarChangeListener {
- @ Override
- Public void onProgressChanged (SeekBar seekBar, int progress,
- Boolean fromUser)
- {
- // TODO Auto-generated method stub
- // Print the position of the current progress bar
- System. out. println (progress );
- }
- @ Override
- Public void onStartTrackingTouch (SeekBar seekBar)
- {
- // TODO Auto-generated method stub
- // The Position of the progress bar when the user starts to drag the slider
- System. out. println ("start -->" + seekBar. getProgress ());
- }
- @ Override
- Public void onStopTrackingTouch (SeekBar seekBar)
- {
- // TODO Auto-generated method stub
- // The Position of the progress bar when the user stops dragging the slider
- System. out. println ("end -->" + seekBar. getProgress ());
- }
- }
- @ Override
- 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;
- }
- }
Layout File
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/hello_world" />
- <SeekBar
- android:id="@+id/seekBar"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </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
- Public class MainActivity extends Activity
- {
- Private RatingBar ratingBar = null;
- @ Override
- Protected void onCreate (Bundle savedInstanceState)
- {
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. activity_main );
- // Find the ratingBar
- RatingBar = (RatingBar) findViewById (R. id. ratingBar );
- // Bind the listener
- RatingBar. setOnRatingBarChangeListener (new RatingBarListener ());
- }
- 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
- System. out. println ("ratingBar" + rating );
- }
- }
- @ Override
- 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;
- }
- }
Layout File
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/hello_world" />
- <RatingBar
- android:id="@+id/ratingBar"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:numStars="5"
- android:stepSize="1.0"
- />
- </RelativeLayout>