I am trying to make custom RatingBar for my android app in which i can change the default size and color of RatingBar. For this purpose i have written styles for CustomRatingBar like this:
<style name="CustomRatingBar" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/custom_ratingbar</item>
<item name="android:minHeight">25dp</item>
<item name="android:maxHeight">25dp</item>
</style>
But the size is not changed rather it shrinks the rating bar like this:
Drawable files are : custom_ratingbar.xml, custom_ratingbar_empty, custom_ratingbar_filled
In my xml, RatingBar looks like below:
<RatingBar
android:id="#+id/RatingBar"
style="#style/CustomRatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:isIndicator="true"
android:numStars="5" />
How can i change the star size of the rating bar without shrinking it?
Just add below lines to your ratingBar in xml layout
android:scaleX="0.5"
android:scaleY="0.5"
Try this ,
custom rating bar style using android’s ratingBar small style.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomRatingBar" parent="#android:style/Widget.RatingBar" >
<item name="android:numStars">5</item>
<item name="android:rating">4.5</item>
<item name="android:stepSize">0.5</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:paddingBottom">44dip</item>
<item name="android:layout_marginLeft">4dip</item>
<item name="android:layout_marginTop">4dip</item>
Check example. for the same.
Other solution is :
Android won't scale a rating bar icons. When you decrease the minHeight and maxHeight android will always crop the icons as shown on the picture. There seems to be the only one solution is to provide your own icons for stars with the desired dimensions and set the minHeight and maxHeight to the size of the icons.
I came across the same issue and even though this question is very old, I hope this will help someone who comes across this kind of scenario.
To increase the size of the stars in the rating bar we need to use android:scaleX and android:scaleY.
Please refer to the following example:
<RatingBar
android:id="#+id/ratingBar"
android:numStars="5"
android:rating="4"
android:isIndicator="true"
android:scaleX=".5"
android:scaleY=".5"
android:transformPivotX="0dp"
android:transformPivotY="0dp"
android:layout_marginBottom="-26dp"
android:layout_marginRight="-100dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="#style/FoursquareReviewRating"/>
Also, you can find more details at https://en.proft.me/2017/06/18/decorating-ratingbar-android/
<..
android:scaleX=".6"
android:scaleY=".6"
//to avoid the unnecessary space
android:transformPivotX="0dp"
../>
Related
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.
I am trying to set a custom color for the small progressbar with this code:
<style name="RatingBar" parent="Theme.AppCompat">
<item name="colorControlNormal">#color/gold_yellow</item>
<item name="colorControlActivated">#color/light_grey</item>
</style>
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="true"
android:rating="3.3"
android:theme="#style/RatingBar"
android:id="#+id/go_rating"
style="#android:style/Widget.Holo.Light.RatingBar.Small"/>
The problem with this is this: the stars are small quite alright but they are blue. But If I remove style="#android:style/Widget.Holo.Light.RatingBar.Small then the color becomes golden yellow but it becomes big.
Please how do I set custom colors for the stars and at the same time make it small?
Use style="?attr/ratingBarStyleSmall" style:
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="true"
android:rating="3.3"
android:theme="#style/RatingBar"
android:id="#+id/go_rating"
style="?attr/ratingBarStyleSmall"/>
see also: https://stackoverflow.com/a/3173594/5183999
I've been trying for days now to style my rating bar and googling how to fix it, when I use the action bar like this:
<RatingBar
android:layout_width="wrap_content"
android:layout_height="25dp"
android:numStars="5"
android:rating="3"
android:isIndicator="true"
android:id="#+id/rating"
style="?android:attr/ratingBarStyleSmall"
/>
I get this result: http://i.imgur.com/MwlxGBz.png (I can't post images yet),
so when I write my own style and do it like:
<RatingBar
android:layout_width="wrap_content"
android:layout_height="25dp"
android:numStars="5"
android:rating="3"
android:isIndicator="true"
android:id="#+id/rating"
style="#style/ratingBar"/>
Where ratingBar is defined in styles.xml as:
<style name="ratingBar" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/ratingstars</item>
</style>
and ratingstars is defined in drawable/ratingstars.xml as:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#android:id/background"
android:drawable="#drawable/staroff"/>
<item
android:id="#android:id/secondaryProgress"
android:drawable="#drawable/staroff"/>
<item
android:id="#android:id/progress"
android:drawable="#drawable/staron"/>
</layer-list>
The staron and staroff are pngs with the desired textures for the stars, the result I get is: http://i.imgur.com/V31dWpK.png
How would I get it to be the same size as the ratingbar in first example, yet use the textures I provided?
I've tried using scaleX and scaleY, which cut the stars and messed up their positioning, setting width is not an option since there'd be more than 5 stars, so what can I do here?
If you want to use custom images you will need to change size of images.
If you want to just change the color of stars there is a possibility to do it with code.
RatingBar ratingBar = (RatingBar)view.findViewById(R.id.rating);
LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
stars.getDrawable(0).setColorFilter(Color.GRAY, PorterDuff.Mode.SRC_ATOP);
stars.getDrawable(1).setColorFilter(Color.GRAY, PorterDuff.Mode.SRC_ATOP);
stars.getDrawable(2).setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_ATOP);
In this example the star that shows rating will be black color and others will be gray.
Don't forget to set the style of RatingBar to small.
Example:
style="#android:style/Widget.DeviceDefault.RatingBar.Small"
In this case you won't need "drawable/ratingstars.xml" and style in "styles.xml".
I'm trying to create a custom RatingBar for one of my apps. I've followed this tutorial: http://kozyr.zydako.net/2010/05/23/pretty-ratingbar/
The problem is that my RatingBar has always the same size and it's always filled. I try to show different ratings (0,1,2,3,4...) but the rating bar it's the same in all the cases...
I have the ratingBar:
<RatingBar
android:id="#+id/ratingBar"
style="#style/MyRatingBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"/>
My Style:
<style name="MyRatingBar" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/rating_list</item>
<item name="android:minHeight">57dip</item>
<item name="android:maxHeight">57dip</item>
</style>
The layer-list:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#android:id/background"
android:drawable="#color/white_text" />
<item android:id="#android:id/secondaryProgress"
android:drawable="#color/white_text" />
<item android:id="#android:id/progress"
android:drawable="#color/red_circle" />
Thank you.
In your <RatingBar> use:
android:numStars to define how many red_circle are used for the full scale of the bar.
android:rating to set the default rating level.
android:stepSize to define the step size of your RatingBar.
<Edit>
I don't believe RatingBar is designed to work using a straight fill color as the progress drawable, which is what you have currently. A color has no dimensions, so drawing 2, 3 or 4 of them has no meaning.
It is designed to work with an image file like the default star png files.
Change your layer-list so the progress drawable points to a Bitmap, or 9Patch, and it works fine. <RatingBar> extends ProgressBar, which uses 9Patchs for the progress drawable.
I am trying to display an int value as a ratingBar. The max value is 5.
This is my XML file:
<RatingBar
android:id="#+id/ratBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="false"
android:numStars="5" />
But if I set the rating with setRating(), android draws a lot of stars. I can't figure out how many because my screen is too small.
Could anybody tell me what's wrong?
I couldn' t understand what exactly your problem is, but using RatingBar in Android applications is quite easy anyway. You need to define your RatingBar in a proper location;
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="true"
android:max="5"
android:numStars="3" />
get a reference to it, within your activity;
private RatingBar rb;
...
this.rb = (RatingBar) findViewById(R.id.ratingBar1);
and start to set values to fill the stars;
this.rb.setRating(2); , this.rb.setRating((float)2.5); etc.
If your screen is too small to display five stars, you can set the number of stars, just as I did above. And you have several options;
1) You can put two ratingbars one under the other one with 3 and the other with 2 stars. When your number exceeds 3, you can continue filling the 2 stars of the bottom ratingbar.
2) If possible, you can set float values instead of integers. So that you can set 1, 1.5, 2, 2.5, 3, 3.5, etc. and you can fill 3 stars for your interval 1-5 after a little algebra.
You need to define your own custom styles to achieve what you want for the RatingBar.
Try out creating the styles and apply that style in your RatingBar view.
Like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="foodRatingBar" parent="#android:style/Widget.RatingBar">
<item name="android:progressDrawable">#drawable/food_ratingbar_full</item>
<item name="android:minHeight">48dip</item>
<item name="android:maxHeight">48dip</item>
</style>
</resources>
<RatingBar android:id="#+id/my_rating_bar"
...
style="#style/foodRatingBar" />
Check out here for more details.
you can try this also
<RatingBar
android:id="#+id/ratingBar"
style="?android:attr/ratingBarStyleIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/artist"
android:layout_below="#+id/artist"
android:numStars="5"
android:rating="2.0"
android:stepSize="1.0" />
This happenswhen the drawable you set in your style for the rating bar has diffent size of png for background, secondary progress and progress.
Change it to something like
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background"
android:drawable="#android:color/transparent" />
<item android:id="#android:id/secondaryProgress"
android:drawable="#drawable/star_half" />
<item android:id="#android:id/progress"
android:drawable="#drawable/star_full" />