Android RatingBar


Download Android RatingBar sample

Android RatingBar is an extension of ProgressBar and SeekBar that shows rating as stars. User can touch/drag or use arrow keys to set the rating when using the default size RatingBar.

The smaller RatingBar style ( ratingBarStyleSmall) and the larger indicator-only style (ratingBarStyleIndicator) doesn't support user interaction and it can be used as indicators only.

In this tutorial, we are going to discuss following things:

  1. Android RatingBar usage
  2. Xml attributes of Android RatingBar
  3. Customization of Android RatingBar
  4. Using RatingBar as indicator
  5. Handling onRatingChanged event of Android RatingBar

Android RatingBar Usage?


Android RatingBar simple usage in xml layout is given below:

Android SeekBar usage

How to set rating in Android RatingBar?


android:rating attribute of Android RatingBar specify rating value that will be shown selected. To set the rating of Android RatingBar in xml layout, set the android:rating attribute of Android RatingBar as follows:

We can also set the rating of an Android RatingBar programmatically as follows:

Rating of an Android RatingBar can have float value.

How to set numStars in Android RatingBar?


android:numStars attribute of Android RatingBar specify number of stars that will be shown in Android RatingBar. To set the number of starts in Android RatingBar, we can set the android:numStars attribute of Android RatingBar in xml layout as follows:

We can also change the numStarts of an Android RatingBar programmatically as follows:

android:numStars

can have only integer value.

How to set stepSize of Android Rating?


android:stepSize of Android Rating specify the step size of the android ratingBar. We can set the stepSize of an Android RatingBar as follows:

android:stepSize can have float value.

How to get rating value of Android RatingBar?


We can get the rating value of an Android RatingBar on runtime as follows:

What is android:isIndicator?


android:isIndicator attribute of Android RatingBar specify whether this rating bar is an indicator. An indicator are non changable by the user.

How to customize Android RatingBar?


We can customize Android RatingBar by defining style under res -> values ->styles.xml as follows:

Next step is to use it in your Android RatingBar as follows:

How to handle onRatingChanged event of Android RatingBar?


To handle onRatingChanged event of Android RatingBar implement RatingBar.OnRatingBarChangeListener in main activity class, and override onRatingChanged method.

Android RatingBar demo

Android SeekBar demo

In this example, We discussed different aspect of Android RatingBar. Now lets implement the demo for Android RatingBar

Step 1: Create a new project in Android Studio and name it Android RatingBar.

Step 2: Open res -> values ->colors.xml and replace its code with below given code:

colors.xml

Step 3: Open res -> values ->styles.xml and replace its code with below given code:

styles.xml

Step 4: Open res -> layout -> activity_main.xml and add following code

activity_main.xml

Step 5: Open your MainActivity.java and replace its source with source of my MainActivity.java as follows:

MainActivity.java

Step 6: Clean and run the project to see the result.


Share & Like

Tutorial topics

About ApnaTutorials.com

ApnaTutorials.com is for Java, J2ee, Html5 and android developers. All tutorials are well explained and are easy to understand. We have tested every tutorials on our developement environment before posting it in public domain.