Vivek Mishra
Vivek Mishra

Reputation: 5705

Styling rating bar not working

I am trying to style my rating bar. I created style for rating bar and added it to my rating bar. But rating bar is ignoring it's style and picking it from AppTheme. Here is both styles

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="colorControlNormal">#ffffff</item>
</style>
    <style name="MyMaterialTheme" parent="AppTheme"/>
    <style name="foodRatingBar" parent="@android:style/Widget.RatingBar">
        <item name="android:progressDrawable">@drawable/rating_stars</item>
        <item name="android:minHeight">48dip</item> <!-- your desired size -->
        <item name="android:maxHeight">48dip</item> <!-- your desired size -->
    </style>

rating_stars.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background"
        android:drawable="@drawable/star_selected" />
    <item android:id="@android:id/secondaryProgress"
        android:drawable="@drawable/star_selected" />
    <item android:id="@android:id/progress"
        android:drawable="@drawable/stsr" />
</layer-list>

rating bar xml

<RatingBar
    android:theme="@style/foodRatingBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="5dp"
    android:numStars="5"
    android:isIndicator="false" />

on applying this style it still picks color from AppTheme: colorCntrolNormal, colorAccent. Is this way is wrong or am I missing something?

Upvotes: 0

Views: 1671

Answers (2)

Alireza Farahani
Alireza Farahani

Reputation: 2539

Change

<RatingBar
android:theme="@style/foodRatingBar"/>

to

<RatingBar
android:theme="@style/MyMaterialTheme"/>

or

<RatingBar
style="@style/foodRatingBar"/>

Currently you are assigning an style to a theme attribute witch doesn't work

Upvotes: 2

Wasim K. Memon
Wasim K. Memon

Reputation: 6067

You can change star color runtime using colorfilter(). try this

LayerDrawable stars = (LayerDrawable) rb_Ratings.getProgressDrawable();
stars.getDrawable(2).setColorFilter(getActivity().getResources().getColor(R.color.primary_alpha_600),
                PorterDuff.Mode.SRC_ATOP);

Upvotes: 1

Related Questions