Android Rating bar
A RatingBar is a widget in Android that allows users to provide a rating for a particular item. It consists of a series of stars that can be clicked on to select a rating value. Here are some important uses and examples of the RatingBar in Android:
Uses:
- Allows users to rate items such as products, services, or content
- Can be used for surveys or feedback forms
- Can provide a visual representation of a rating or score
Example:
To use a RatingBar in Android, you can follow these steps:
- Add the RatingBar element to your layout XML file:
- In your activity or fragment, create a reference to the RatingBar:
- Optionally, you can set the rating programmatically:
- To get the current rating value, you can use the getRating() method:
- You can also set a listener to be notified when the rating changes:
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="0.5"
android:rating="2.5" />
RatingBar ratingBar = findViewById(R.id.ratingBar);
ratingBar.setRating(3.5f);
float rating = ratingBar.getRating();
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
// Do something when the rating changes
}
});
This is a basic example of how to use a RatingBar in Android. The RatingBar can be customized further by using custom drawables or styles.
Next Article ❯ Android - SeekBar