0

I wanted to use RatingBar with my own two images. I am searching for solution but still failing. There is topic with best answer here: https://stackoverflow.com/a/5800686/7329859

I decided to create new project to implement only things for RatingBar like solution from previous topic. Project here: https://github.com/Wiktorl4z/ratingbar

ratingbar.xml

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

ratingbar_empty.xml

  <?xml version="1.0" encoding="utf-8"?>

<!-- This is the rating bar drawable that is used to
 show a filled cookie. -->
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true"
        android:state_window_focused="true"
        android:drawable="@drawable/folder" />

    <item android:state_focused="true"
        android:state_window_focused="true"
        android:drawable="@drawable/folder" />

    <item android:state_selected="true"
        android:state_window_focused="true"
        android:drawable="@drawable/folder" />

    <item android:drawable="@drawable/folder" />

</selector>

ratingbar_filled.xml

<?xml version="1.0" encoding="utf-8"?>

<!-- This is the rating bar drawable that is used to
show a unfilled cookie. -->
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true"
        android:state_window_focused="true"
        android:drawable="@drawable/camera" />

    <item android:state_focused="true"
        android:state_window_focused="true"
        android:drawable="@drawable/camera" />

    <item android:state_selected="true"
        android:state_window_focused="true"
        android:drawable="@drawable/camera" />

    <item android:drawable="@drawable/camera" />

</selector>

styles.xml

   <style name="CustomRatingBar" parent="Widget.AppCompat.RatingBar">
        <item name="android:progressDrawable">@drawable/ratingbar</item>
        <item name="android:minHeight">50dp</item>
        <item name="android:maxHeight">50dp</item>
    </style>

activity.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <RatingBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style= "@style/CustomRatingBar"
        android:id="@+id/ratingBar"
        android:numStars="9" />

</LinearLayout>

This is how it display

After spending few hours I decided to ask on stackoverflow, what I do wrong?

  • why is your `ratingbar_empty.xml` the same with `ratingbar.xml`? Did you paste the wrong file? If not then that's a possible cause of the problem. – Meow Cat 2012 Dec 13 '18 at 09:46
  • Yes I made mistake when copy paste from github to stackoverflow. Thanks for pointing out @MeowCat2012 But problem is still open. – Wiktor Kalinowski Dec 13 '18 at 09:49

2 Answers2

1

so the big thing happens in ratingbar.xml You have to change all

<item android:id="@+id/ to

<item android:id="@android:id/"

After this change everything is working.

0

Try to use the png image. RatingBar doesn't work as well with svg.

Vova
  • 956
  • 8
  • 22