Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Rating Bar shows only full stars, not half stars too

I'm using rating bars in my Android application. But it only shows full stars, when I also want half stars.

I inserted a SQLite row with a name and the ratingValue, in there the ratingValue stands as "3.5" but my application turns it into "4". Even if my StepSize is "0.5", even the default value is changed to "3"

EDIT: If you click on the listrow, it dialogs another ratingbar which you can "rate" and if you click on a button, it sends the rating value into the database. The listitem RatingBar is only for showing the "average" rating but that's where it goes wrong, it rounds the value up

EDIT 2: set the stepsize to 0.1 and now, strangely, only the middle star accepts the float number... The rest of the stars are still only full And if i set rating on 1.2, only the second star accepts float value, 0.2 the first, etc but the other stars (not in that range) are only or full or empty

Here's some code

ListItem_layout

<TableRow>    
    <RatingBar
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/sportsRatingBar"
        android:gravity="center_horizontal"
        android:focusableInTouchMode="false"
        android:isIndicator="true"
        android:clickable="false"
        android:focusable="false"
        android:layout_column="1"
        android:rating="1.1"
        android:stepSize="0.1" />

ListItem

if(ratingBar!=null) {
                ratingBar.setRating(p.getRating());
                ratingBar.setTag(p.getId());
            }

Reusable class

public float getRating() { return Rating; }

SQLite Table Column for rating

 COLUMN_RATING + " real not null);"

And last, getting from database

c.getFloat(COLUMN_RATING_COL));

some images to explain after setting first ratingbar to 2.6

like image 308
sander338 Avatar asked Feb 29 '16 16:02

sander338


2 Answers

I think you don't need any library for this, I just tried it, and it working.

<android.support.v7.widget.AppCompatRatingBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="?android:attr/ratingBarStyle"
    android:background="@android:color/holo_red_dark"
    android:id="@+id/ratingBar1"
    android:stepSize="0.1"
    android:rating="2.5"/>

I set value of start to 4.5 and it is working that way.

RatingBar ratingBar = (RatingBar) findViewById(R.id.coloredRatingBar1);
    ratingBar.setRating(4.8f);
    ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
            Log.i(TAG, "onRatingChanged: rating : "+rating);
        }
    });
like image 112
Dave Ranjan Avatar answered Oct 04 '22 22:10

Dave Ranjan


First of all the the way rating widget is defined it cannot accept values. Clickable is set to false and isIndicator is made true. To accept input define the widget in the following way

<RatingBar
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/sportsRatingBar"
        android:gravity="center_horizontal"
        android:rating="2.5"
        android:stepSize="0.5" />

For the getting the values not sure about how the variable Rating is being set

Check out the image for how the widget looks like!

like image 45
Umang Avatar answered Oct 04 '22 23:10

Umang