AppCompatRatingBar

AppCompatRatingBar is used to get the rating from the app user. A user can simply touch, drag or click on the stars to set the rating value. As AppCompatRatingBar is advance version of RatingBar so we recommend to use AppCompatRatingBar.

public class AppCompatRatingBar extends RatingBar
java.lang.Object
↳ android.view.View
  ↳ android.widget.ProgressBar
    ↳ android.widget.AbsSeekBar
      ↳ android.widget.RatingBar
        ↳ androidx.appcompat.widget.AppCompatRatingBar

We Recommend to use new UI components i.e. androidx components


Methods

Method Description
getRating() You can get the rating number from a AppCompatRatingBar by using getRating() method. This method returns a Floating Point number. Below we get the current rating number from a AppCompatRatingBar.
getNumStars() You can get the number of stars of a AppCompatRatingBar by using getNumstars() method. This method returns int value. In below code we get the total number of stars of a AppCompatRatingBar.
setRating(float rating) Set the default rating of a rating bar. It must be a floating point number.

Create ratingbar_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.appcompat.widget.AppCompatRatingBar
        android:id="@+id/rateBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:rating="4.5" />

</RelativeLayout>

Create RatingBarActivity.java
package com.theitbulls.uicomponentex;

import android.os.Bundle;
import android.os.Handler;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatRatingBar;

public class RatingBarActivity extends AppCompatActivity {
    private AppCompatRatingBar rateBar;

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

        rateBar = findViewById(R.id.rateBar);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                rateBar.setRating(3.5f);
            }
        }, 2000);
    }
}