Android RatingBar

Accordingly to the official documentation, a RatingBar is an extension of SeekBar and ProgressBar that shows a rating in stars.

Android RatingBar – basic example

Code:

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

Result:

Read-only RatingBar

It is used just to show current rating. If “isIndicator” is false it will allow the user to edit the rating.

Code:

<RatingBar
    android:id="@+id/rating_bar_indicator"
    style="@style/Widget.AppCompat.RatingBar.Indicator"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/space_16"
    android:numStars="5"
    android:rating="3.5"
    android:stepSize="0.5" />

Result:

Small RatingBar

By default it will act just like Indicator one, but if “isIndicator” is false it will allow the user to edit the rating.

Code:

<RatingBar
    android:id="@+id/rating_bar_small"
    style="@style/Widget.AppCompat.RatingBar.Small"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/space_16"
    android:isIndicator="false"
    android:numStars="5"
    android:rating="3.5"
    android:stepSize="0.5" />

Result: (the 3rd one)

RatingBar Change Listener

Code:

RatingBar ratingBar = findViewById(R.id.rating_bar_basic);

ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
    @Override
    public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
        Toast.makeText(RatingBarActivity.this,
                getString(R.string.current_rating) + ratingBar.getRating(),
                Toast.LENGTH_SHORT).show();
    }
});

Result:

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s