I am trying to dynamically through Kotlin add a list of products with ratingbars for their ratings. I was able to add the ratingbars; however, the rating bars show 7 stars. I want 5 stars. I tried using *.numStars = 5 but I still get 7 stars as shown in the pic below for Popcorn.
fun populateUIWithRatingInfo() {
for (orderItem in orderItemsList) {
val dynamicTextView = TextView(requireContext())
dynamicTextView.text = orderItem.title
binding.productRatingList.addView(dynamicTextView)
val dynamicRatingBar = RatingBar(requireContext())
dynamicRatingBar.numStars= 5
binding.productRatingList.addView(dynamicRatingBar)
}
}
Answer: The answer marked with the check mark is correct below. Since it doesn't describe how to do that dynamically, I will post a snippet of code that shows how to set width to wrap-content of the rating bar.
dynamicRatingBar.layoutParams = LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
CodePudding user response:
According to the android documentation, you need to set the layout_width
property to wrap_content
public void setNumStars (int numStars): Sets the number of stars to show. In order for these to be shown properly, it is recommended the layout width of this widget be wrap content.