RatingBar with small white stars - android

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" />

Related

Android rating baar not showing properly

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"

Android: Color of stars in RatingBar, that already uses style

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"

How to apply RatingBar border without using an image?

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!

How to remove the RatingBar star border stroke in Android?

I am using RatingBar in my application. For this rating bar app star I am getting the border stroke. How can I remove this border stroke? Below are my xml and screenshot of the bar.
<RatingBar
android:id="#+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyleSmall"
android:numStars="5"
android:stepSize="0.8"
android:scaleX="0.8"
android:scaleY="0.8"
android:isIndicator="true"
android:layout_marginStart="60dp"
android:layout_marginEnd="17dp"
android:layout_marginTop="4dp"
android:layout_below="#+id/userName" />
you need custom your ratingbar and provide your own images,default ratingbar's image contain border stroke:
<style name="RatingBarStyle" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/ratingbar_selector</item>
<item name="android:minHeight">34dp</item>
<item name="android:maxHeight">34dp</item>
</style>
<item android:id="#+android:id/background"
android:drawable="#drawable/emotion_composer_rating_small_icon" />
<item android:id="#+android:id/secondaryProgress"
android:drawable="#drawable/emotion_composer_rating_small_icon" />
<item android:id="#+android:id/progress"
android:drawable="#drawable/emotion_composer_rating_small_icon_highlighted" />

How to give spacing between rating bar stars android?

Hi I am using custom rating bar with custom images. I need to provide space between stars. When I increase minimum height and maximum height, star image remains same.
<RelativeLayout
android:id="#+id/RelativeLayout01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="5dp">
<TextView
android:id="#+id/allreviews"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Rating"
android:layout_marginLeft="8dp"
android:textSize="14sp" />
<RatingBar
android:id="#+id/reviewallrating"
style="#style/customRatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="3dp"
android:layout_toRightOf="#+id/allreviews"
android:isIndicator="false"
android:numStars="5"
android:stepSize="0.1" />
</RelativeLayout>
styles:
#drawable/star_rating_bar_full
12dip
12dip
star_rating_bar_full.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_ratingbar_full_empty" />
<!-- <item android:id="#+android:id/secondaryProgress"
android:drawable="#drawable/star_ratingbar_full_empty" /> -->
<item android:id="#+android:id/progress"
android:drawable="#drawable/star_ratingbar_full_filled" />
</layer-list>
star_ratingbar_full_empty:
<?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/star_gray" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="#drawable/star_gray" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="#drawable/star_gray"/>
<item android:drawable="#drawable/star_gray" />
</selector>
star_ratingbar_fullfilled.xml:
<item android:state_pressed="true"
android:state_window_focused="true"
android:drawable="#drawable/yellow_star" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="#drawable/yellow_star" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="#drawable/yellow_star" />
<item android:drawable="#drawable/yellow_star" />
I used style="?android:attr/ratingBarStyle" in styles but still output remains same.
You could simply add some empty space to the left and right sides of your PNGs.
That is, increase the width of the grey and yellow PNGs in GIMP (or whatever graphic editor program you use).
Just the canvas, without stretching the star icon.
If you are using Drawable for the stars. Make sure your drawable have some left padding according to your requirement in the png image.
Otherwise through code its not working. Some days back I stuck with the same problem. I gone through by using the left padded image. Hope it will work.

Categories

Resources