In this article I am going to explain how to create rating bar control in an android application by using RatingBar widget. When a user click on the rating bar, a new rating will be displayed by the toast message.

·         Start a new project, named RatingBarDemo.

·         Open res/layout/main.xml and add a RatingBar widget inside the linear layout.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=""
     <RatingBar android:id="@+id/ratingbar"
                  android:stepSize="1.0" />

·         Now we have to show the new rating to the user by using toast message. To do this add the following code in the activity:


import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.Toast;
import android.widget.RatingBar.OnRatingBarChangeListener;

public class RatingBarActivity extends Activity {

     public void onCreate(Bundle savedInstanceState) {
         final RatingBar ratingbar = (RatingBar) findViewById(;
         ratingbar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

                      public void onRatingChanged(RatingBar ratingBar, float rating,
                                                                boolean fromUser) {

                           Toast.makeText(RatingBarActivity.this, "New Rating:
                                           "+rating, Toast.LENGTH_SHORT).show();

The above code captures the RatingBar widget from the layout with findViewById(int) and then sets an RatingBar.OnRatingBarChangeListener. The onRatingChanged() callback method then defines the action to perform when the user sets a rating. In this case, a simple Toast message displays the new rating.

·         Run the application

You output look something like this:

Rating Bar control in Android

When users select a new rating, a rating will be displayed in the toast message.

  Modified On Mar-23-2018 02:07:08 AM

Leave Comment