I am following the standard example of how to add a RatingBar
. To control the number of stars I tried to use android:numStars=\"5\"
. The problem is
For me, I had an issue until I removed the padding. It looks like there is a bug on certain devices that doesn't alter the size of the view to accommodate the padding, and instead compresses the contents.
Remove padding
, use layout_margin
instead.
If you are wrapping the RatingBar
inside a ConstraintLayout
with match_constraint
for its width, the editor preview is going to show a number of stars proportional to its actual width, no matter what if you set android:numStars
property. Use wrap_content
to get the correct preview:
<RatingBar android:id="@+id/myRatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="48dp"
android:layout_marginEnd="48dp"
android:numStars="5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent" />
You can add default rating of five stars in side the xml layout
android:rating="5"
Edit:
<RatingBar
android:id="@+id/rb_vvm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="@dimen/space2"
android:layout_marginTop="@dimen/space1"
style="@style/RatingBar"
android:numStars="5"
android:stepSize="1"
android:rating="5" />
I'm also facing the problem that exceeding the stars than specified no of stars. For this, we don't want worry about whatever the layout type either relative or linear layout. Simply use the width as follows:
ratingBar.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Please avoid using match_parent and fill_parent for ratingbar.
Hope the things would be helpful.
The actual thing over here is to use wrap_content
instead of match_parent
. If you will use match_parent
the layout will be filled with the stars even if it is more than the numStars variable.
Additionally, if you set a layout_weight
, this supersedes the numStars
attribute.