Android RatingBar Example

In this tutorial you will learn about Android RatingBar with example.

RatingBar is widget in android that is used to show user rating in the form of stars. The user can drag or touch it to set the rating value. Rating bar is very import widget specially for eCommerce apps where we have to display user rating.

To use rating bar in android just add a <RatingBar> tag in layout xml file. Some of its important attributes are given below.

Attribute Java Method Description
android:rating setRating(float rating) Used to assign default rating value.
android:numStars setNumStars(int numStars) Used to set number of starts in rating bar. If you use this attribute then make sure rating bar width is wrap content.
android:isIndicator setIsIndicator(boolean) If boolean value is true then rating bar act as indicator and user can’t change its value. Rating bar is changeable if boolean value is false. By default rating bar is changeable.
android:stepSize setStepSize(float stepSize) Used to set rate of increase and decrease. If step size is 1.0 then rating bar value will increase or decrease by 1.0.

Use getRating() method to get the current value of rating bar. Apply setOnRatingBarChangeListener to rating bar to get rating value whenever it changes.

Below example show how to implement rating bar in android.

Android RatingBar Example

Make an android studio project with package name com.androidratingbar.

Now add following code in respective files.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:padding="15dp"
    tools:context="com.androidratingbar.MainActivity"
    android:orientation="vertical">

    <RatingBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ratingBar"
        android:stepSize="1.0"
        android:numStars="5"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Your Rating: "
        android:id="@+id/ratingText"/>
</LinearLayout>

MainActivity.java

package com.androidratingbar;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    RatingBar ratingBar;
    TextView ratingText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ratingBar = (RatingBar)findViewById(R.id.ratingBar);
        ratingText = (TextView)findViewById(R.id.ratingText);

        ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                ratingText.setText("Your Rating: " + rating);
            }
        });
    }
}

Save and run the project.

When you will touch or drag your finger over rating bar then its value will be changed and displayed in textview.

Screenshot

Android RatingBar Example

Comment below if you have queries or found anything incorrect in above android rating bar example.

Leave a Comment

Your email address will not be published. Required fields are marked *