![]() |
VOOZH | about |
Android RatingBar is a user interface widget which is used to get the rating from the customers or users. It is an extension of SeekBar and Progress Bar that shows star ratings and it allow users to give the rating by clicking on the stars.
In RatingBar, we can set the step size using android:stepSize and it will always return a rating value as floating point number such as 1.0, 2.0, 2.5 etc. By using, android:numStars attribute we can specify the number of stars in RatingBar. RatingBar is used to get ratings form users or customers about the product, movie or hotel experience etc. RatingBar can be created manually or programmatically but we are going to discuss programmatically or dynamically.
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Note: Select Kotlin as the programming language.
In this file, we use the Constraint Layout and a Linear Layout inside it where we will add the Rating Bar and set its attributes like id, padding etc and it can be accessed in the Kotlin file using id.
activity_main.xml:
First of all, we declare variable ratingBar to create RatingBar and set its attributes using it.
val ratingBar = RatingBar(this)
val layoutParams = LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
ratingBar.layoutParams = layoutParams
ratingBar.numStars = 5
After this, add RatingBar into the Linear Layout using the statements
layout.addView(ratingBar)MainActivity.kt: