Unable to change rating bar color - android

I'm using the following code for rating bar.
<RatingBar
android:id="#+id/ratingBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:stepSize="1.0"
android:background="#color/yellow"/>
The problem is that I'm unable to change the color of stars in rating bar. Even when i tried setting background. How can I change the color of star?

Try it....
android:progressTint="#color/color"
or
you can use it...
Use android:theme attribute:
styles.xml
<style name="Theme.Rating" parent="Theme.AppCompat.Light">
<item name="colorAccent">#color/rating</item>
</style>
layout.xml
<android.support.v7.widget.AppCompatRatingBar
android:theme="#style/Theme.Rating"
android:numStars="5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

i would suggest create three star images yello_full.png, yellow_half.png and yellow_empty.png . now put these images in drawable folder.
Create a xml file in drawable and put 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/yellow_empty" />
<item
android:id="#android:id/secondaryProgress"
android:drawable="#drawable/yellow_half" />
<item
android:id="#android:id/progress"
android:drawable="#drawable/yello_full" />
</layer-list>
now in your rating bar add this
<RatingBar
android:progressDrawable="#drawable/ratingbar_yellow"/>
and here you go.. Enjoy coding

Instead of creating drawables you can change it by setting color filter.
Example
ratingWidget.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
Here color is the color you want to set to the widget.

Related

android ratingbar small style

i tried to create a small rating bar something like this
<RatingBar
android:id="#+id/ratingBar"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="10dp"
android:numStars="5"
android:rating="3.5" />
and its working perfectly ,, now i need to replace star with custom image like this
<style name="CustomRatingBar" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/ratingbar</item>
</style>
ratingbar.xml
<?xml version="1.0" encoding="utf-8"?>
<!--suppress AndroidDomInspection -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+android:id/background"
android:drawable="#drawable/ic_love_empty" />
<item android:id="#+android:id/secondaryProgress"
android:drawable="#drawable/ic_love_empty" />
<item android:id="#+android:id/progress"
android:drawable="#drawable/ic_love_fill" />
</layer-list>
the problem here is with the rating bar size ,, its very big and cropped how i can replace #android:style/Widget.RatingBar with this ?android:attr/ratingBarStyleSmall ,, or if any other solution or library its ok for me but i need small
I once had a problem like yours whereby android seems not to consider the resizing of rating bars. All the styles I applied was not small enough to my use case. I ended up using the third party libraries.
The first one or The second one These libraries will help you resizing the rating bars. Because android parent styles of rating bars are hard to resize and do not have default implementation. Happy Coding!
Set your parent style to #style/Widget.AppCompat.RatingBar.Small instead.

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 use selectors when background attribute is used for icon?

As you can see, the background attribute is used for a icon, so I cant use drawable selectors on that attribute. I have also tried to use a selector inside a style.xml and use the style attribute to call it, but without any succses.
What can I do?
<ImageButton
android:id="#+id/blackButton"
android:background="#drawable/black_icon"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_centerHorizontal="true"
android:onClick="onButtonClicked"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
You can put your .png drawable inside your selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/black_icon_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/black_icon_focused"
android:state_focused="true" />
<item android:drawable="#drawable/black_icon_normal" />
</selector>
You will maybe have to create another image from your black_icon drawable in order to create the desired effect.

Spacing between stars in Custom Rating Bar

Spacing between the stars of a rating bar?
I have similar problem but I didn't find solution to make it without changes of images.
Maybe someome faced with such problem and solved it.
Thanks in advance.
Create a custom style that uses your own images with different spacing between them.
Use it in a layout like this:
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/RatingBarLarge"/>
Define the style like this:
<style name="RatingBarLarge">
<item name="android:progressDrawable">#drawable/ic_rating_star_large</item>
<item name="android:minHeight">29dp</item>
</style>
Set the minHeight to whatever the height of your drawable is.
Then, define ic_rating_star_large as a layer list like this:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#android:id/background"
android:drawable="#drawable/ic_rate_star_large_off" />
<item
android:id="#android:id/secondaryProgress"
android:drawable="#drawable/ic_rate_star_large_off" />
<item
android:id="#android:id/progress"
android:drawable="#drawable/ic_rate_star_large_on" />
</layer-list>
Are you using this same kind of code for your rating bar?
// ?android:attr/ratingBarStyle- normal star
// ?android:attr/ratingBarStyleSmall - small star
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="#android:drawable/toast_frame">
<RatingBar android:layout_width="wrap_content"
android:layout_height="wrap_content" style="?android:attr/ratingBarStyle"
android:id="#+id/ratingbar_default"
android:layout_marginTop="5dp"
android:layout_marginLeft="-8dp"
/>
</RelativeLayout>

Categories

Resources