Skip to content
Related Articles

Related Articles

Android | Creating a RatingBar
  • Last Updated : 23 Feb, 2021

RatingBar is used to allow the users to rate some products. In the below code getRating() function is used to calculate the rating of the products. The getRating() function returns double type value.

Below steps are involved to create a RatingBar in Android:

  1. Create a new android project.
  2. Add RatingBar in your activity_main.xml.
  3. Add Button to invoke action.
  4. Use TextView to display the ratings.
  • To use the rating bar in the app, we will use the in-built RatingBar widget, hence the first step is to import it into the project.
  • In the MainActivity, make the RatingBar object denoted by the variable ‘rt’ and find its corresponding view in the XML file. This is done by the findViewById() method. After the java object has successfully bind to its view, create the ‘stars’ layout, which the user will interact with, to set the rating.
  • To get the drawable stars, the method rt.getProcessDrawable() is used. Then to modify the colours of the stars, the method setColorFilter() is used and the argument Color.YELLOW is passed. Finally, the Call method is written to extract the value of the rating that the user has selected, by the method rt.getMethod().

Program to create MainActivity:

// Below is the code for
package com.example.hp.rating;
// importing required libraries
import android.os.Bundle;
import android.view.View;
import android.widget.RatingBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
RatingBar rt;
    protected void onCreate(Bundle savedInstanceState) {
        //binding with activity_main.xml file
        rt = (RatingBar) findViewById(;
        //finding the specific RatingBar with its unique ID
        LayerDrawable stars=(LayerDrawable)rt.getProgressDrawable();
       //Use for changing the color of RatingBar
        stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
    public void Call(View v)  
        // This function is called when button is clicked.
        // Display ratings, which is required to be converted into string first.
        TextView t = (TextView)findViewById(;
        t.setText("You Rated :"+String.valueOf(rt.getRating()));

Note: For the layout, ConstraintLayout is good to use if you are a beginner because it can adjust the views as per the screens.
This XML file defines the view of the application.

Program to create layout for MainActivity:

<?xml version="1.0" encoding="utf-8"?>
    <!-- Cover the entire width of the screen -->
    <!-- Cover the entire height of the screen -->
        tools:layout_constraintTop_creator="1" />
        android:text="Rate Me!!!"
        tools:layout_editor_absoluteY="28dp" />
        tools:layout_constraintLeft_creator="1" />
        tools:layout_constraintTop_creator="1" />

Here we don’t need to change the manifest file, no permission is required for the ratingBar. By default, all the created new activities are mentioned in the manifest file.

Below is the code for AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
    package="com.example.hp.rating" >
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />


Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :