RatingBar is used to get the rating from the app user. A user can simply touch, drag or click on the stars to set the rating value. The value of rating always returns a floating point number which may be 1.0, 2.5, 4.5 etc.
In Android, RatingBar is an extension of ProgressBar and SeekBar which shows a rating in stars. RatingBar is a subclass of AbsSeekBar class.
The getRating() method of android RatingBar class returns the rating number.
Below is the example of RatingBar in Android where we displayed a RatingBar with five stars and a submit button. Whenever a user click on the button value of total number of stars and value of rating is shown by using a Toast on screen. Below is the final output, download code and step by step tutorial:
Important Note: We can also create custom rating bar in Android in which we can change the star images like filled or empty star. We discussed this in next example, so don’t miss it.
Step 1: Create a new project and name it RatingBarExample
Step 2: Open res -> layout -> activity_main.xml (or) main.xml and add following code
In this step we open an xml file and add the code for displaying a rating bar with five number of stars and “2” value for default rating and one submit button.
<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">
<RatingBar
android:id="@+id/simpleRatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:background="#0f0"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:rating="2" />
<Button
android:id="@+id/submitButton"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#f00"
android:padding="10dp"
android:text="Submit"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
Step 3: Open src -> package -> MainActivity.java
In this step we open MainActivity where we add the code to initiate the RatingBar & button and then we perform click event on button and display the total number of stars and rating by using a toast.
package example.gb.ratingbarexample;
import ...
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initiate rating bar and a button
final RatingBar simpleRatingBar = (RatingBar) findViewById(R.id.simpleRatingBar);
Button submitButton = (Button) findViewById(R.id.submitButton);
// perform click event on button
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// get values and then displayed in a toast
String totalStars = "Total Stars:: " + simpleRatingBar.getNumStars();
String rating = "Rating :: " + simpleRatingBar.getRating();
Toast.makeText(getApplicationContext(), totalStars + "\n" + rating, Toast.LENGTH_LONG).show();
}
});
}
}