How to use RatingBar in android?

Before getting into an example, we should know what is rating bar in android. Rating bar is a subclass of absSeekbar class in android. It is used to show the rating on view Group or window manager.

This example demonstrates how to use the rating bar in android.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
      android:text="Get Rating "/>

In the above code, we have declared Rating bar and set numStars as 5 means it allows maximum number of stars 5 and button. User has to drag stars for rating and click on button when user click on button it will print stars rating on toast.

Step 3 − Add the following code to src/

package com.example.andy.myapplication;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
   protected void onCreate(Bundle savedInstanceState) {
      Button getRating = findViewById(;
      final RatingBar ratingBar = findViewById(;
      getRating.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
            String rating = "Rating is :" + ratingBar.getRating();
            Toast.makeText(MainActivity.this, rating, Toast.LENGTH_LONG).show();

In the above code, we have declare button onclick listener, when you click button it going to get rating as show below -

String rating = "Rating is :" + ratingBar.getRating();

Step 4 − No need to change manifest.xml

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from an android studio, open one of your project's activity files and click Run Icon  from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

The above result indicates initial screen, User has to drag rating on rating bar and click on button as shown below-

The above result we have selected rating as 3 and it is printed as 3.

The above result we have selected rating 3.5 and printed as 3.5

Click here to download the project code

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started