Android RatingBar Example

ANDROID-RATINGBAR-EXAMPLE-1.png

11th October, 2022

Rating bar is used to get the rating from the user. A user can simply touch and drag on the Rating bar to set rating value. Here you will see a simple example of Android RatingBar.

Step 1: Create New Project

Create a new project in Android Studio from File ⇒ New Project and select Empty Activity from the templates.

Step 2: Add RatingBar in Layout XML

Now open the layout file and add the RatingBar in it.

<?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:orientation="vertical"
    tools:context=".MainActivity"
    android:gravity="center">

    <RatingBar
        android:id="@+id/ratingBar2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5"
        android:rating="3.5" />

    <Button
        android:id="@+id/buttonSubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit" />

</LinearLayout>

Step 3: Using the RatingBar in the Activity

After adding the RatingBar to the layout file, open the related activity and modify it with the code below.

package com.codestringz.mytestapp;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity
{

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

        setUIRef();
    }

    private void setUIRef()
    {
        //Get UI Ref. of RatingBar
        final RatingBar ratingBar = findViewById(R.id.ratingBar2);

        //Get UI Ref. of Submit Button
        Button submitButton = findViewById(R.id.buttonSubmit);

        //Handle Click event of Submit Button
        submitButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //Get stars from RatingBar
                int stars = ratingBar.getNumStars();

                //Get rating from RatingBar
                float rating = ratingBar.getRating();

                String myStr = "Total Stars: " + stars + "\n" + "Rating: " + rating;

                //Display a toast
                Toast.makeText(MainActivity.this, myStr, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Useful Methods

RatingBar onRatingChanged Listener

This is a useful method to handle the changes in the RatingBar. You can register rating change listener. Please See the following example.

<?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:orientation="vertical"
    tools:context=".MainActivity"
    android:gravity="center">

    <TextView
        android:id="@+id/hintTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Rating: "
        android:textSize="22sp"/>

    <RatingBar
        android:id="@+id/ratingBar2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5"
        android:rating="3.5" />
</LinearLayout>
package com.codestringz.mytestapp;

import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity
{

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

        setUIRef();
    }

    private void setUIRef()
    {
        //Get UI Ref. of TextView
        final TextView hintTextView = findViewById(R.id.hintTextView);

        //Get UI Ref. of RatingBar
        final RatingBar ratingBar = findViewById(R.id.ratingBar2);

        ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener()
        {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser)
            {
                if (fromUser)
                {
                    //--This block will be executed if the rating is changed by the user--//

                    String myStr = "Rating: " + rating + "\nBy User";

                    hintTextView.setText(myStr);
                }
                else
                {
                    //--This block will be executed if the rating is changed by the code--//
                    
                    String myStr = "Rating: " + rating + "\nBy Code";

                    hintTextView.setText(myStr);
                }
            }
        });

        //Let's set rating by code
        ratingBar.setRating(4.5f);
    }
}

Happy coding!

Leave a Reply