How can I decrease the size of Ratingbar?
Asked Answered
R

18

175

In my activity I have some Rating bars. But the size of this bar is so big! How can I make it smaller?

Edit

Thanks to Gabriel Negut, I did it with the following style:

<RatingBar
style = "?android:attr/ratingBarStyleSmall"
android:numStars = "5"
android:rating   = "4" />

Now, the size is reduced but number of stars and rating do not take effect!!! Why? I have 7 stars that 6 of them is selected.

Revanche answered 27/5, 2011 at 14:21 Comment(3)
Do you mean a Seekbar? Setting height and width works fine for me. Can you post your code snippet here?Tshombe
I would recommend removing the "edit" and instead making it the accepted answer. That or @GabrielNegut can edit his answer to include that kind of solution. Including the solution in the question kind of takes away from the Q&A experience.Lipo
style="?android:attr/ratingBarStyleSmall" works for me.Dehydrate
P
215

The original link I posted is now broken (there's a good reason why posting links only is not the best way to go). You have to style the RatingBar with either ratingBarStyleSmall or a custom style inheriting from Widget.Material.RatingBar.Small (assuming you're using Material Design in your app).

Option 1:

<RatingBar
    android:id="@+id/ratingBar"
    style="?android:attr/ratingBarStyleSmall"
    ... />

Option 2:

// styles.xml
<style name="customRatingBar"   
    parent="android:style/Widget.Material.RatingBar.Small">
    ... // Additional customizations
</style>

// layout.xml
<RatingBar
    android:id="@+id/ratingBar"
    style="@style/customRatingBar"
    ... />
Prefab answered 27/5, 2011 at 14:34 Comment(3)
or using this style style="?android:attr/ratingBarStyleSmall" will reduce the size.Bismuthous
But there is a useless extra padding on the right! How to remove that?!Medullated
Note: make width="wrap_content" to get rid of extra starsDetestation
P
107

This was my solution after a lot of struggling to reduce the rating bar in small size without even ugly padding

 <RatingBar
        android:id="@+id/listitemrating"
        style="@android:attr/ratingBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleX=".5"
        android:scaleY=".5"
        android:transformPivotX="0dp"
        android:transformPivotY="0dp"
        android:isIndicator="true"
        android:max="5" />
Peake answered 3/7, 2015 at 12:45 Comment(1)
Scale bothers alignment of sibling views and imbalances margins and spacing.Margaret
F
68

If you want to show the rating bar in small size, then just copy and paste this code in your project.

  <RatingBar
    android:id="@+id/MyRating"
    style="?android:attr/ratingBarStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/getRating"
    android:isIndicator="true"
    android:numStars="5"
    android:stepSize="0.1" />
Fulford answered 25/6, 2014 at 11:50 Comment(8)
What is android:stepSize="0.1" ?Vallo
codingaffairs.blogspot.com/2016/05/…Hearsay
also check my asnwer at the end of this page.Hearsay
@HammadTariqSahi It is good, keep it up, But is there any problem in my code? Let me know if there is any thing, pleaseFulford
@PirFahimShah thankyou, bro I havent checked this code. you can improve and can style rating bar if you follow that approach.Hearsay
@HammadTariqSahi Ok, i will surely look at itFulford
@can you please fix this issue #37383665Fulford
@PirFahimShah what if I want the ratingBarStyleSmall style AND also I want to style the stars' color to yellow please?Playwright
A
50

The below snippet worked for me to resize the ratingBar

<RatingBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ratingBar"
    style="?android:attr/ratingBarStyleIndicator"
    android:scaleX=".5"
    android:rating="3.5"
    android:scaleY=".5"
    android:transformPivotX="0dp"
    android:transformPivotY="0dp"
    android:max="5"/>

enter image description here

Agitate answered 3/12, 2016 at 20:37 Comment(6)
how to change the ratting bar color for this rating bar half fill, full fill, and empty color ?Rewire
rating parameter is used to fill the rating bar right? I used android:rating="3.5" so 3.5 stars are filled. you can set up to 5 because max = 5 is set in the xml. If you want to change the colors of the rating bar, you have to create a style in style.xml see this link : https://mcmap.net/q/144432/-android-change-color-of-ratingbar-to-golden-duplicateAgitate
how to android:transformPivotX="0dp" android:transformPivotY="0dp" Programmatically ?Varnish
@NaveedAhmad But there is a useless padding on the right! How to remove that?!Medullated
@Mr.Hyde check the styles of raringbar https://mcmap.net/q/142236/-how-can-i-decrease-the-size-of-ratingbarAgitate
@NaveedAhmad It doesn't do any related thing to padding! Or at least, I couldn't find it. github.com/aosp-mirror/platform_frameworks_base/blob/master/… -> progressDrawable, indeterminateDrawable, minHeight, maxHeight.Medullated
S
46

You can set it in the XML code for the RatingBar, use scaleX and scaleY to adjust accordingly. "1.0" would be the normal size, and anything in the ".0" will reduce it, also anything greater than "1.0" will increase it.

<RatingBar
  android:id="@+id/ratingBar1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:scaleX="0.5"
  android:scaleY="0.5" />
Selfrevealing answered 3/3, 2014 at 4:8 Comment(3)
the problem with this is that the ratingbar still takes up the same width and height as before even though the icons are smaller. This causes alignment issues since there is a bunch of left and right padding next to the first and last icons. For example, if you want your icons like this "XXXXX", after the scaling it will look like this "-----XXXXX-----" where the "-" is empty space and "X" is an icon. So your rating bar will seem like it got pushed to the right because of the empty spacePampas
Im guessing you're using relative layout. Its a pain but you can tweak it to go into the right position by using android:layout_marginRight and the likes to get it the way you want.Selfrevealing
to get scale without creating a padding on the left add android:transformPivotX="0dp"Vallo
H
12

Working Example

             <RatingBar
                style="@style/RatingBar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:numStars="5"
                android:rating="3.5"
                android:stepSize="0.5" />

and add this in your styles xml file

<style name="RatingBar"   
parent="android:style/Widget.Material.RatingBar.Small">
<item name="colorControlNormal">@color/primary_light</item>
<item name="colorControlActivated">@color/primary_dark</item>
</style>

This way you dont need to customise ratingBar.

Hearsay answered 18/5, 2016 at 17:5 Comment(0)
E
10

Check my answer here. Best way to achieve it.

 <style name="MyRatingBar" parent="@android:style/Widget.RatingBar">
   <item name="android:minHeight">15dp</item>
   <item name="android:maxHeight">15dp</item>
   <item name="colorControlNormal">@color/white</item>
   <item name="colorControlActivated">@color/home_add</item>
</style>

and use like

<RatingBar
    style="?android:attr/ratingBarStyleIndicator"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:isIndicator="false"
      android:max="5"
      android:rating="3"
      android:scaleX=".8"
      android:scaleY=".8"
      android:theme="@style/MyRatingBar" />
Equation answered 17/4, 2018 at 11:35 Comment(0)
F
8

This Worked for me.
Check this image

            android:id="@+id/ratingBar"
            style="?android:attr/ratingBarStyleIndicator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:numStars="5"
            android:scaleX=".8"
            android:scaleY=".8"
            android:stepSize="0.5"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.543"
            app:layout_constraintStart_toEndOf="@+id/title"
            app:layout_constraintTop_toTopOf="parent" />
Federative answered 25/7, 2020 at 4:46 Comment(0)
P
8

This will work perfectly

style="?android:attr/ratingBarStyleSmall"
Paramount answered 18/6, 2022 at 9:53 Comment(0)
A
7
<RatingBar
  style="?android:attr/ratingBarStyleIndicator"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
/>
Accomplishment answered 18/8, 2015 at 12:32 Comment(2)
Could you elaborate on how you arrived at the solution?Lipo
This doesn't allow RatingBar to get changed.Sequential
R
6

In the RatingBar tag, add these two lines

style="@style/Widget.AppCompat.RatingBar.Small"
android:isIndicator="false"
Rump answered 6/10, 2019 at 2:22 Comment(0)
R
5

In RatingBar give attribute:

style="?android:attr/ratingBarStyleIndicator" 
Radiophotograph answered 10/7, 2015 at 6:41 Comment(0)
L
5

Using Widget.AppCompat.RatingBar, you have 2 styles to use; Indicator and Small for large and small sizes respectively. See example below.

<RatingBar
    android:id="@+id/rating_star_value"
    style="@style/Widget.AppCompat.RatingBar.Small"
    ... />
Lehr answered 30/8, 2018 at 15:49 Comment(0)
S
4

If you are using Rating bar in Linearlayout with weightSum. Please make sure that you should not assign the layout_weight for rating bar. Instead, that place rating bar inside relative layout and give weight to the relative layout. Make rating bar width wrap content.

<RelativeLayout
    android:layout_width="0dp"
    android:layout_weight="30"
    android:layout_gravity="center"
    android:layout_height="wrap_content">
        <RatingBar
            android:id="@+id/userRating"
            style="@style/Widget.AppCompat.RatingBar.Small"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:stepSize="1" />
</RelativeLayout>
Sudorific answered 29/6, 2018 at 13:8 Comment(0)
M
4
 <RatingBar     android:id="@+id/id_tv_rating_bar"
                style="@style/Widget.AppCompat.RatingBar.Small"
                android:layout_width="wrap_content"
                android:layout_height="@dimen/_20sdp"
                android:layout_marginLeft="@dimen/_80sdp"
                android:numStars="5"
                android:paddingTop="@dimen/_5sdp"
                android:rating="5" />

Just use style="@style/Widget.AppCompat.RatingBar.Small" it'll work for Sure!

Margaret answered 5/7, 2020 at 11:2 Comment(1)
after using this rating bar color is not changing. How to change it?Chamberlin
C
2

If you only need the default style, make sure you have the following width/height, otherwise the numStars could get messed up:

android:layout_width="wrap_content"
android:layout_height="wrap_content"
Cockeyed answered 2/1, 2014 at 20:16 Comment(1)
using this style style="?android:attr/ratingBarStyleSmall" will reduce the sizeBismuthous
E
0

For those who created rating bar programmatically and want set small rating bar instead of default big rating bar


    private LinearLayout generateRatingView(float value){
        LinearLayout linearLayoutRating=new LinearLayout(getContext());
        linearLayoutRating.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        linearLayoutRating.setGravity(Gravity.CENTER);
        RatingBar ratingBar = new RatingBar(getContext(),null, android.R.attr.ratingBarStyleSmall);
        ratingBar.setEnabled(false);
        ratingBar.setStepSize(Float.parseFloat("0.5"));//for enabling half star
        ratingBar.setNumStars(5);
        ratingBar.setRating(value);
        linearLayoutRating.addView(ratingBar);
        return linearLayoutRating;
    }

Employee answered 11/12, 2018 at 11:7 Comment(0)
G
0
 <RatingBar
                    android:id="@+id/ratingBarDisplay"
                    style="?android:attr/ratingBarStyleSmall"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:numStars="5"
                    android:scaleX="1.3"
                    android:scaleY="1.3"
                    android:transformPivotX="0dp"
                    />
Glyconeogenesis answered 17/9, 2023 at 7:18 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Sulcus

© 2022 - 2024 — McMap. All rights reserved.