Rating Bar In Android

Introduction

 
Rating Bar is a commonly used control in Android applications for showing some ratings about some items in the activity or allowing the user to set ratings about something. We all find there is a rating bar in the Google Play store for rating every application. This is exactly the same control. This control we can use in our application also for rating something.
 
Here I am going to show you how we can use this control and how we can set and get values from this control. Here I am going to demonstrate an Activity with one TextView, Rating Bar and a Button. The TextView will show the current rating of the Rating Bar, the user can change the rating on clicking in the rating bar. And on clicking on the button the I will show a Toast message about the current rating;  this is what I am going to show you.
 
First, I am going to create the main layout. The main layout is a Relative layout and I added a TextView at the top center of the screen. Now I added the Rating Bar at the center of the screen. The rating bar shows some attributes like numStars, rating, stepSize. numStars means the number of starts we need in the rating control, most commonly used is five. But here we can change this as we like. The rating means the initial rating of the control, that is if the initial rating is 2 then on running this application it will show a rating bar with 2 stars selected. stepSize means how many steps we want to go forward and backward on clicking it. The main layout will look like the following.
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  2. xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"    
  3. android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"    
  4. android:paddingRight="@dimen/activity_horizontal_margin"    
  5. android:paddingTop="@dimen/activity_vertical_margin"    
  6. android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">    
  7.     <TextView android:text="@string/hello_world" android:layout_width="wrap_content"    
  8. android:layout_height="wrap_content"    
  9. android:id="@+id/textView"    
  10. android:layout_alignParentTop="true"    
  11. android:layout_centerHorizontal="true" />    
  12.     <RatingBar    
  13. android:layout_width="wrap_content"    
  14. android:layout_height="wrap_content"    
  15. android:id="@+id/ratingBar"    
  16. android:layout_centerVertical="true"    
  17. android:layout_centerHorizontal="true"    
  18. android:numStars="6"    
  19. android:rating="2"    
  20. android:stepSize="1" />    
  21.     <Button    
  22. android:layout_width="wrap_content"    
  23. android:layout_height="wrap_content"    
  24. android:text="New Button"    
  25. android:id="@+id/button"    
  26. android:layout_below="@+id/ratingBar"    
  27. android:layout_centerHorizontal="true"    
  28. android:layout_marginTop="82dp" />    
  29. </RelativeLayout>   
    In the MainActivity we will initialise all these control and create a listner for rating changed event.
    1. ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener()     
    2. {    
    3.     @Override    
    4.     public void onRatingChanged(RatingBar ratingBar, float v, boolean b)    
    5.     {    
    6.         textView.setText("New Rating is: " + v);    
    7.     }    
    8. });   
      and in the button click we will show a toast about the rating,
      1. button.setOnClickListener(new View.OnClickListener()     
      2. {    
      3.     @Override    
      4.     public void onClick(View view)    
      5.     {    
      6.         Toast.makeText(MainActivity.this"New Rating is " + ratingBar.getRating(), Toast.LENGTH_SHORT).show();    
      7.     }    
      8. });   
        Please see the output also.
         
         
        Read more articles on Android