I'm trying to make the un-filled stars in the RatingBar an opaque color, but it seems like a tall task consisting of tint lists and PorterDuff Modes. Is there a rock solid way to make the un-filled stars of a RatingBar a truely opaque color?
val color = ContextCompat.getColor(context, android.R.color.white)
ratingBar.secondaryProgressTintList = ColorStateList.valueOf(white)
ratingBar.secondaryProgressTintMode = PorterDuff.Mode.OVERLAY //whats the sauce?
The un-filled color is still transparent, and the background of the view is half white!
First, change progress color of rating bar. Its done by two ways, either by applying theme on RatingBar or by manually using LayerDrawable. That's it. android:stepSize="0.5" />LayerDrawable stars = (LayerDrawable) rating.
Custom Rating Bar in AndroidCopy your images in the drawable folder and remember image size should be according to the size you want. Step 2. Make XML for the rating bar selector in the drawable folder like below. We made two files, one for the fill or highlighted part and one for the empty or un-highlighted part.
Step 1: After opening the android studio and creating a new project with an empty activity. Step 2: Navigate to res/values/colors. xml, and add a color that you want to change for the status bar. Step 3: In your MainActivity, add this code in your onCreate method.
The stars in a RatingBar
are simply a LayerDrawable
. The key to setting the color of unfilled stars is to get a handle on the index within the LayerDrawable
that contains a description of the drawable for unfilled stars (index = 0). Once we have that drawable, we can color it with "Drawable#setTint()" for API 21+ or with "Drawable#setColorFilter()" for API <21.
The following example sets the unfilled stars to an opaque green:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RatingBar ratingBar = findViewById(R.id.ratingBar);
LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
int unfilledColor = getResources().getColor(android.R.color.holo_green_dark);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
stars.getDrawable(0).setTint(unfilledColor);
} else {
stars.getDrawable(0).setColorFilter(unfilledColor, PorterDuff.Mode.SRC_ATOP);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With