I wanted to create a RatingBar that does not use an image to have a border. But I can't seem to get it right.
Here's what I wanted it to look like:
Here's what I got (filled - half empty - empty):
Here's my layout:
<RatingBar
android:id="#+id/ratingbar_rate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:numStars="5"
android:rating="2.5"
android:stepSize="0.5"
android:scaleX="3.5"
android:scaleY="3.5"
style="?android:attr/ratingBarStyleIndicator"
android:isIndicator="false"
android:progressTint="#color/star"
android:progressBackgroundTint="#color/star_border"
android:secondaryProgressTint="#color/star_partially_empty"
android:backgroundTint="#color/star_partially_empty"
/>
Maybe any of these additional libraries: http://android-arsenal.com/tag/84
would do a disired work for you. Notice, that some of these doesn't use image.
Hope it help
Okay, so I used a custom drawable layer-list and Android will be the one to do the rest.
My RatingBar:
<RatingBar
android:id="#+id/ratingbar_rate"
style="#style/rateUsBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:rating="0"
android:stepSize="1"
android:layout_gravity="center"
/>
In styles.xml:
<resources>
...
...
<style name="rateUsBar" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/rating_stars</item>
<item name="android:minHeight">#dimen/rate_us_star_minheight</item>
<item name="android:maxHeight">#dimen/rate_us_star_maxheight</item>
</style>
</resources>
In 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/rate_empty" /> //gray star for empty
<item
android:id="#android:id/secondaryProgress"
android:drawable="#drawable/rate_empty" /> //gray star for empty
<item
android:id="#android:id/progress"
android:drawable="#drawable/rate_filled" /> //yellow star for filled
</layer-list>
In dimens.xml:
<resources>
...
...
<dimen name="rate_us_star_minheight"> <!--height of your image--> </dimen>
<dimen name="rate_us_star_maxheight"> <!--height of your image--> </dimen>
</resources>
And that's it!
Related
I am facing a strange problem with my custom RatingBar drawable. My images of drawables are stretched as you can see in the image.
I don't know what is the issue. I tried different solutions from here but none of them helped me.
Here is my rating bar xml code
<RatingBar
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:maxHeight="30dp"
android:numStars="5"
android:progressDrawable="#drawable/ratingbar_custom"
android:rating="4"
android:stepSize="1" />
here is drawable 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_empty" />
<item
android:id="#android:id/secondaryProgress"
android:drawable="#drawable/star_empty" />
<item
android:id="#android:id/progress"
android:drawable="#drawable/star_filled" />
</layer-list>
And these are 2 images star_empty and star_filled
Ratingbar star not showing properly.
i don't know what i am doing wrong with it.
when i used custom style then a single star is showing only and its length is equal to 5star.
<RatingBar
android:id="#+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="-15dp"
android:stepSize="1"
android:numStars="5"
style="#style/customRatingBar" />
Style is:
<style name="customRatingBar" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/manual_ratingbar</item>
<item name="android:minHeight">32dip</item>
<item name="android:maxHeight">32dip</item>
</style>
manual_ratingbar file:
<?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/ic_star_unselect" />
<item android:id="#android:id/progress" android:drawable="#drawable/ic_star_select" />
</layer-list>
it looks like:
and when i changed the Ratingbar style to
style="?attr/ratingBarStyleIndicator"
then there is extra star visible in background.
what should i do?
#chandan you can resolve this issue by adding this on RatingBar Layout android:indeterminate="false"
The following is the xml for my rating bar.
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyleSmall"
android:numStars="5"
android:isIndicator="true"
android:stepSize="0.5"
android:rating="4.5"/>
I am happy with its size but I would like to stars to be white on grey (white for filled and grey for empty).
I am looking at this tutorial (http://www.materialdoc.com/rating-bar/) but I am not sure how to adapt the style to suit the small appearance.
You can try drawable
<?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_off" />
<item android:id="#android:id/secondaryProgress"
android:drawable="#drawable/star_off" />
<item android:id="#android:id/progress"
android:drawable="#drawable/star_on" />
</layer-list>
<RatingBar
android:id="#+id/ratingBar2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:progressDrawable="#drawable/custom_ratingbar_selector"
android:isIndicator="true"
android:stepSize="0.5"
android:rating="4.5" />
you can use progressTint for this purpose
<RatingBar
android:id="#+id/vendorRatingBarValue"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignRight="#+id/reviewerName"
android:clickable="false"
android:gravity="left|center"
android:isIndicator="true"
android:numStars="5"
android:progressTint="#color/white"
android:transformPivotX="0dp"
/>
you would need to use the attributes
android:backgroundTint="#color/grey"
android:progressTint="#color/white"
so..
<RatingBar
android:id="#+id/rb_doctor"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:backgroundTint="#color/grey"
android:numStars="5"
android:progressTint="#color/white"
android:rating="0"
android:secondaryProgressTint="#color/white"
android:stepSize="1.0"
/>
This is simple example of RatingBar.
Custom white and gray star can possible with custom theme for this and specify small size so size of rating bar star will be same as default small size theme.
You have to create drawable for custom rating bar and make sure about the size of rating bar because here height of rating bar matters a lot.i mean if your star image is size of 13dp height and rating bar is 17dp height than it will stretch the rating bar so make sure about height.
Here is simple example of how can create custom selector for rating bar.
Star Drawable
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:state_window_focused="true" android:drawable="#drawable/ic_star_empty_small" />
<item android:state_focused="true" android:state_window_focused="true" android:drawable="#drawable/ic_star_empty_small" />
<item android:state_selected="true" android:state_window_focused="true" android:drawable="#drawable/ic_star_empty_small" />
<item android:drawable="#drawable/ic_star_empty_small" />
</selector>
Style for rating bar
<style name="foodRatingBar" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/inf_full_ratingbar</item>
<item name="android:minHeight">20dip</item>
<item name="android:width">20dip</item>
<item name="android:height">20dip</item>
<item name="android:maxHeight">20dip</item>
</style>
And than use custom style for rating bar like:
<RatingBar
android:id="#+id/ratingBarFlight"
style="#style/StarRatingBar"
android:layout_width="wrap_content"
android:layout_height="#dimen/rating_bar_height"
android:layout_gravity="center_horizontal"
android:layout_marginTop="#dimen/library_screen" />
I want to change colour of RatingBar stars in every mode(highlighted, notHighlited, progress), but I already use style Widget.AppCompat.RatingBar.
I don't want to change style cause, I like star shapes and staff, I only want to change star colors, Is it possible?
Here's my code:
<RatingBar
android:id="#+id/ratingBar"
style="#style/Widget.AppCompat.RatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1.0"
android:layout_gravity="center_horizontal"
android:rating="0.0" />
Use styles:
<?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/background" />
<item android:id="#android:id/secondaryProgress" android:drawable="#drawable/secondary_progress" />
<item android:id="#android:id/progress" android:drawable="#drawable/progress" />
</layer-list>
You can use this style following way. Add this line to the RatingBar:
android:progressDrawable="#drawable/rating"
Xml file:
<RatingBar
android:id="#+id/row_item_rbRatingAvg"
style="#style/CustomRatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:isIndicator="true" />
Style file:
<style name="CustomRatingBar" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/ratingstars</item>
<item name="android:minHeight">#dimen/rating_bar_height</item>
<item name="android:maxHeight">#dimen/rating_bar_height</item>
</style>
ratingstars.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_icon_a" />
<item android:id="#+android:id/secondaryProgress"
android:drawable="#drawable/star_icon_a" />
<item android:id="#+android:id/progress"
android:drawable="#drawable/star_icon" />
</layer-list>
dimens.xml
<dimen name="rating_bar_height">38px</dimen>
above files i used for set my custom ratingbar. i have also different size images for star_icon_a and star_icon. but still not working. in some devices, image get scaled and in some device image get cut. i dont know how to make for all screen sizes.
below is sample of drawable-xxhdpi size.