Android: Change color of ratingbar to golden
This option is now included in the AppCompat library.
The AppCompat version of the RatingBar is used automatically.
http://developer.android.com/reference/android/support/v7/widget/AppCompatRatingBar.html
Example (from my own app):
<RatingBar
android:id="@+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1"
android:theme="@style/RatingBar"/>
With theme:
<style name="RatingBar" parent="Theme.AppCompat">
<item name="colorControlNormal">@color/lightGrey</item>
<item name="colorControlActivated">@color/duskYellow</item>
</style>
Android RatingBar change star colors
Step #1: Create your own style, by cloning one of the existing styles (from $ANDROID_HOME/platforms/$SDK/data/res/values/styles.xml
), putting it in your own project's styles.xml
, and referencing it when you add the widget to a layout.
Step #2: Create your own LayerDrawable
XML resources for the RatingBar
, pointing to appropriate images to use for the bar. The original styles will point you to the existing resources that you can compare with. Then, adjust your style to use your own LayerDrawable
resources, rather than built-in ones.
Change ratingbar color in android
try following code programatically
RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
How to set the colors of Android small RatingBar
Use style="?attr/ratingBarStyleSmall"
style:
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="true"
android:rating="3.3"
android:theme="@style/RatingBar"
android:id="@+id/go_rating"
style="?attr/ratingBarStyleSmall"/>
see also: https://stackoverflow.com/a/3173594/5183999
Change or remove RatingBar focus color
On API 21 and higher, you can change the color of the filled stars with
android:progressTint="@color/color"
and the color of the stroke with
android:progressBackgroundTint="@color/color"
Make the RatingBar secondary color opaque
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);
}
}
}
Related Topics
Creating an Android Trial Application That Expires After a Fixed Time Period
How to Get an Event in Android Spinner When the Current Selected Item Is Selected Again
Android Mediaplayer Error (1, -2147483648)
Round Corner for Bottomsheetdialogfragment
How to Add a Dropdown Item on the Action Bar
Recognizing Multiple Keywords Using Pocketsphinx
Out of Memory Error with Bitmap
Apply a Theme to an Activity in Android
Sharing Data Between Fragments Using New Architecture Component Viewmodel
How to Sign an Already Compiled APK
Android Emulator Not Able to Access the Internet
Android Set Up Volley to Use from Cache
Handle Screen Rotation Without Losing Data - Android
Deleting Android Sms Programmatically