Madhukar Hebbar
Madhukar Hebbar

Reputation: 3173

Android rating bar set star dimension

I am displaying rating bar in my application where i want to reduce the dimensions of the star. so here is xml of rating bar

       <RatingBar
                android:id="@+id/ratingBar"
                style="?android:attr/ratingBarStyleSmall"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="false"
                android:max="5"
                android:stepSize="0.25"
                android:numStars="0"
                android:focusable="false"
                android:layout_gravity="center_horizontal"
                android:focusableInTouchMode="false"
                android:isIndicator="true"/>

i am setting color and setting rating like this

 private RatingBar ratingBar;
 Drawable ratingBarColor = ratingBar.getProgressDrawable();
ratingBar = (RatingBar) view.findViewById(R.id.ratingBar);
 Drawable ratingBarColor = ratingBar.getProgressDrawable();
ratingBar.setRating((float) (value));
 DrawableCompat.setTint(ratingBarColor, ContextCompat.getColor(getActivity(), R.color.red));

But the problem is regardless of any value the color and decimal filling won't work.I am using style to reduce star dimensions.strange is if i remove style attribute in xml it will set exact value as star but dimension will reset to default.if i add min/max height to star the image will get cropped. here is the same problem :- How to set the custom size of the stars in RatingBar tried :style="?android:attr/ratingBarStyleIndicator" still same issue. i am not getting what's wrong with the style attribute.

Upvotes: 1

Views: 1740

Answers (1)

FlyingPumba
FlyingPumba

Reputation: 1053

I don't know if it will be useful anymore, but I made a custom library which allows you to change star dimension programatically (among other stuff): SimpleRatingBar.

It features:

  • Fully working android:layout_width: it can be set to wrap_content, match_parent or abritary dp.
  • Arbitrary number of stars.
  • Arbitrary step size.
  • Size of stars can be controlled exactly or by setting a maximum size.
  • Customizable colors in normal state (border, fill and background of stars and rating bar).
  • Customizable colors in pressed state (border, fill and background of stars and rating bar).
  • Customizable size separation between stars.
  • Customizable border width of stars.
  • Customizable stars corner radius.
  • Allows to set OnRatingBarChangeListener
  • Stars fill can be set to start from left to right or from right to left (RTL language support).
  • AnimationBuilder integrated in the view to set rating programatically with animation.

Here is a preview of it.

In your case, you would just have to do:

ratingbar.setStarSize(20, Dimension.DP);

or, for example, in pixels:

ratingbar.setStarSize(100, Dimension.PX);

You can find it either in jcenter or in Maven Central. So in your build.gradle file just add to your dependencies:

compile 'com.iarcuschin:simpleratingbar:0.1.+'

Upvotes: 1

Related Questions