2

I stack with very annoing problem, I want to add custom drawable to my RatingBar widget, I followed some examples how to do it:

1) I created my own style, set minHeight=16dip and maxHeight=16dip

2) Added 16px*16px star drawables.

It changed icon, but the problem is that my stars looks blur, i.e low resolution. And I know why, bcs drawables are 16*16px, but I cant make their qualitity better (for example 76px*76px), bcs Android won't scale a rating bar icons. When you decrease the minHeight and maxHeight android will always crop the icons. Even if I dont use min and max heght result is the same - my big drawables (I tried to add drawable in hdpi,xhdpi folders) just crop.

Is there a way how to increase resolution of my drawables in custom RatingBar? Thx.

P.S. here is my code just in case:

Style:

<style name="CustomRatingBar" parent="@android:style/Widget.RatingBar">
    <item name="android:progressDrawable">@drawable/rating_bar_drawable</item>
    <item name="android:minHeight">16dip</item>
    <item name="android:maxHeight">16dip</item>
</style>

rating_bar_drawable.xml:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background" android:drawable="@drawable/car_washes_star_empty" />
<item android:id="@android:id/secondaryProgress" android:drawable="@drawable/car_washes_star_fill" />
<item android:id="@android:id/progress" android:drawable="@drawable/car_washes_star_fill" />

main.xml:

 <RatingBar
            android:id="@+id/car_washes_list_item_rating_bar"
            style="@style/CustomRatingBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="@dimen/car_washes_item_rating_bar_margin_right"
            android:layout_marginRight="@dimen/car_washes_item_rating_bar_margin_right"
            android:numStars="5"/>
Community
  • 1
  • 1
Panich Maxim
  • 1,105
  • 1
  • 15
  • 34

2 Answers2

3

i also tried to use the custom rating style once in android and my experience was bad so i learnt that it is better to use the native sytles and then cutomize the color in your class file as below

 <RatingBar
    android:id="@+id/ratingBar"
    android:isIndicator="false"
    android:numStars="5"
    android:stepSize="1.0"
    style="?android:attr/ratingBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="45dp"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/linearLayoutLoginFailed"
    android:progressTint="@android:color/white"
   />

and in your class if you want to change the color of yoru rating stars add this code

LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable();

    // Unfilled star color
   DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(2)),
            Color.WHITE);
Mehroz Munir
  • 2,248
  • 1
  • 18
  • 16
2

Try this example

  1. Create custom_ratingbar_selector.xml in drawable and paste this code

    <?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/unslected_star_icon" />
    
    <item android:id="@android:id/secondaryProgress"
    android:drawable="@drawable/unslected_star_icon" />
    
    <item android:id="@android:id/progress"
    android:drawable="@drawable/slected_star_icon" />
    
    </layer-list>
    
  2. use this in your layout activity

    <RatingBar
     android:id="@+id/ratingBar2"
     android:layout_width="wrap_content"
     android:layout_height="21dp"
     android:progressDrawable="@drawable/custom_ratingbar_selector"
     android:numStars="5"
     android:stepSize="1"
     android:rating="3.0" />
    

    Note:

    my pic's dimen is 22*21 so i take height of rating bar is 21dp....check ur pic's size and set as height if u dont want to displays unaccurately

Sunil
  • 3,785
  • 1
  • 32
  • 43