Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture RatingBar Click

I seem to have a problem with catching my ratingbar click. The ratings bar is showing up just fine and has the default value. The only problem is that I cannot change any values or it isn't enabled. I have tried numerous different things (e.g. enabling in the layout, building it entirely in java). None of them seem to have an impact. Here is my latest incarnation of the ratings bar. I must be doing something stooopid to not be able capture the click.

Java Code:

  RatingBar showRatingBar = (RatingBar) findViewById(R.id.showRatingBar);
    showRatingBar.setEnabled(true);
    showRatingBar.setClickable(true);
    showRatingBar.setRating(0);
    showRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener(){
        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating,
                boolean fromUser) {
            System.out.println("showRating.buildRatingBar:  " +rating);
            ratingBar.setRating(rating);

        }});
    showRatingBar.refreshDrawableState();

Layout:

         <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/showQualityLabel"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="@string/show_rating_label"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#E6E6E6"
            android:textSize="12sp" />

        <RatingBar
            android:id="@+id/showRatingBar"
            style="?android:attr/ratingBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:max="5"
            android:numStars="5"
            android:rating="0"
            android:stepSize="1"/>
    </LinearLayout>

Thank you in advance.

Craig

like image 893
mornindew Avatar asked Nov 23 '12 20:11

mornindew


People also ask

How to use RatingBar in android studio?

RatingBar is used to get the rating from the app user. A user can simply touch, drag or click on the stars to set the rating value. The value of rating always returns a floating point number which may be 1.0, 2.5, 4.5 etc.

What is Rating bar?

android.widget.RatingBar. A RatingBar is an extension of SeekBar and ProgressBar that shows a rating in stars. The user can touch/drag or use arrow keys to set the rating when using the default size RatingBar.


1 Answers

setOnClickListener() not working is that RatingBar overrides onTouchEvent() and never let View take care of it, so View#performClick() is never called (which would have called the OnClickListener).

derive from RatingBar and override onTouchEvent()

ratingBar.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                     float touchPositionX = event.getX();
                     float width = ratingBar.getWidth();
                     float starsf = (touchPositionX / width) * 5.0f;
                     int stars = (int)starsf + 1;
                     ratingBar.setRating(stars);

                     Toast.makeText(MainActivity.this, String.valueOf("test"), Toast.LENGTH_SHORT).show();                   
                     v.setPressed(false);
                }
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    v.setPressed(true);
                }

                if (event.getAction() == MotionEvent.ACTION_CANCEL) {
                    v.setPressed(false);
                }




                return true;
            }});
like image 118
Talha Avatar answered Oct 03 '22 23:10

Talha