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.
android:attr/ratingBarStyleSmall" or scaleX/scaleY the click interaction with RatingBar is disabled. This works for me. Hope this helps you!
How to glue the code given here ...
Step-1. You need your own rating stars in res/drawable
...
Step-2 In res/drawable
you need ratingstars.xml
as follow ...
<?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_empty" />
<item android:id="@android:id/secondaryProgress"
android:drawable="@drawable/star_empty" />
<item android:id="@android:id/progress"
android:drawable="@drawable/star" />
</layer-list>
Step-3 In res/values
you need styles.xml
as follow ...
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="foodRatingBar" parent="@android:style/Widget.RatingBar">
<item name="android:progressDrawable">@drawable/ratingstars</item>
<item name="android:minHeight">22dip</item>
<item name="android:maxHeight">22dip</item>
</style>
</resources>
Step-4 In your layout ...
<RatingBar
android:id="@+id/rtbProductRating"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:numStars="5"
android:rating="3.5"
android:isIndicator="false"
style="@style/foodRatingBar"
/>
The default RatingBar widget is sorta' lame.
The source makes reference to style "?android:attr/ratingBarStyleIndicator
" in addition to the "?android:attr/ratingBarStyleSmall
" that you're already familiar with. ratingBarStyleIndicator
is slightly smaller but it's still pretty ugly and the comments note that these styles "don't support interaction".
You're probably better-off rolling your own. There's a decent-looking guide at http://kozyr.zydako.net/2010/05/23/pretty-ratingbar/ showing how to do this. (I haven't done it myself yet, but will be attempting in a day or so.)
Good luck!
p.s. Sorry, was going to post a link to the source for you to poke around in but I'm a new user and can't post more than 1 URL. If you dig your way through the source tree, it's located at frameworks/base/core/java/android/widget/RatingBar.java
Just use:
android:scaleX="0.5"
android:scaleY="0.5"
Change the scale factor according to your need.
In order to scale without creating a padding on the left also add
android:transformPivotX="0dp"
There is no need to add a listener to the ?android:attr/ratingBarStyleSmall
. Just add
android:isIndicator=false
and it will capture click events, e.g.
<RatingBar
android:id="@+id/myRatingBar"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:isIndicator="false" />
the small one implement by the OS
<RatingBar
android:id="@+id/ratingBar"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
apply this.
<RatingBar android:id="@+id/indicator_ratingbar"
style="?android:attr/ratingBarStyleIndicator"
android:layout_marginLeft="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
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