What is Rating Bar
A RatingBar is an extension of SeekBar and ProgressBar that shows a rating in stars. The user can touch/drag or use arrow keys to set the rating when using the default size RatingBa
In Android, you can use “android.widget.RatingBar” to display rating bar component in stars icon. The user is able to touch, drag or click on the stars to set the rating value easily.
A RatingBar is an extension of SeekBar and ProgressBar that shows a rating in stars. The user can touch/drag or use arrow keys to set the rating when using the default size RatingBa
In Android, you can use “android.widget.RatingBar” to display rating bar component in stars icon. The user is able to touch, drag or click on the stars to set the rating value easily.
Android RatingBar displays the rating in stars. Android RatingBar is the subclass of AbsSeekBar class.
The getRating() method of android RatingBar class returns the rating number
Lets create a project and use Rating bar
main.xml
- <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity" >
- <RatingBar
- android:id="@+id/ratingBar1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="44dp" />
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/ratingBar1"
- android:layout_below="@+id/ratingBar1"
- android:layout_marginLeft="92dp"
- android:layout_marginTop="66dp"
- android:text="submit" />
- </RelativeLayout>
MainActivity.xml
- public class MainActivity extends Activity {
- RatingBar ratingbar1;
- Button button;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- addListenerOnButtonClick();
- }
- public void addListenerOnButtonClick(){
- ratingbar1=(RatingBar)findViewById(R.id.ratingBar1);
- button=(Button)findViewById(R.id.button1);
- //Performing action on Button Click
- button.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View arg0) {
- //Getting the rating and displaying it on the toast
- String rating=String.valueOf(ratingbar1.getRating());
- Toast.makeText(getApplicationContext(), rating, Toast.LENGTH_LONG).show();
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.activity_main, menu);
- return true;
- }
- }
Now run the Project, you will see how rating bar works.
No comments:
Post a Comment