---
title: "Rating Bar control in Android"  
description: "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 r"  
author: "Chris Anderson"  
published: 2011-10-24  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/791/rating-bar-control-in-android  
category: "android"  
tags: ["android"]  
reading_time: 3 minutes  

---

# Rating Bar control in Android

In this article I am going to [explain](https://www.mindstick.com/forum/159699/what-is-a-compilation-error-in-dot-net-core-explain-with-an-example) how to create rating bar [control](https://yourviews.mindstick.com/view/83439/bipartisan-gun-control-bill-passed-in-us-after-decades) in an [android application](https://www.mindstick.com/articles/1653/creating-libraries-for-android-applications) 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](https://yourviews.mindstick.com/story/1788/things-to-ensure-your-construction-project-is-successful), 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="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">
     <RatingBar android:id="@+id/ratingbar"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:numStars="5"
                  android:stepSize="1.0" />
</LinearLayout>
```

· 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](https://www.mindstick.com/forum/23119/how-to-save-activity-state-in-android):

```
import android.app.Activity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.Toast;
import android.widget.RatingBar.OnRatingBarChangeListener;

public class RatingBarActivity  extends Activity {

     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         final RatingBar ratingbar = (RatingBar) findViewById(R.id.ratingbar);
         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](https://www.mindstick.com/forum/215/callback-method-in-jquery) 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](https://www.mindstick.com/mindstickarticle/f474917a-ed19-41d9-9a73-c537013b716b/images/4d1dbaa9-7f3b-4baf-ad73-0e45c54a4859.png)

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

\

---

Original Source: https://www.mindstick.com/articles/791/rating-bar-control-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
