Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom view RatingBar has setOnTouchListener called on it but does not override performClick

I have used RatingBar in my layout as 1 star like below -

<RatingBar
            android:id="@+id/ratingBar"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_below="@id/textViewReleaseDate"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            android:layout_toRightOf="@id/imageViewPoster"
            android:numStars="1"
            android:stepSize="1.0" />

And have setOnTouchListener in my activity like below -

ratingBar.setOnTouchListener(new View.OnTouchListener() {

        int ratingAtActionDown;

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN)
                ratingAtActionDown = (int) ratingBar.getRating();
            else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                ratingBar.setRating(ratingAtActionDown == 0 ? 1 : 0);
            }

            return true;
        }
    });

For above snippet I am getting this warning -

Custom view 'RatingBar' has setOnTouchListener called on it but does not override performClick

enter image description here

On Android Studio 2.3.3 ratingBar.setOnTouchListener wasn't generating warning but after uprading to Android Studio 3.0 stable it started warning.

What should be done to get rid of the warning?

like image 602
Hrishikesh Kadam Avatar asked Oct 26 '17 10:10

Hrishikesh Kadam


1 Answers

Lint seems to wrongly think that any view not implementing the performClick() method is a custom view. Knowing this, we can guess that the views affected by this warning are, in fact, missing that implementation.

Now to answer your question, you may need to extend the View you want to set a onTouchListener if you want the warning to disappear:

class TouchableRatingBar extends android.support.v7.widget.AppCompatRatingBar{

    public TouchableRatingBar(Context context) {
        super(context);
    }
    @Override
    public boolean performClick() {
        return true;
    }
}

Override the performClick() method and you should be good to go.

Note that i used the AppCompatRatingBar as Lint seems to dislike doing otherwise.

You also may need to either double cast the Rating Bar or change it's type in the layout.

Double Cast:

TouchableRatingBar ratingBar = (TouchableRatingBar)(RatingBar)findViewById(R.id.ratingBar);

I wouldn't use the double cast personally, but if you need to have an alternative to simply changing the type in the layout it might do the job.

Type Change:

<yourcompany.yourproject.TouchableRatingBar
            android:id="@+id/ratingBar"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_below="@id/textViewReleaseDate"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            android:layout_toRightOf="@id/imageViewPoster"
            android:numStars="1"
            android:stepSize="1.0" />
like image 54
spfi Avatar answered Nov 13 '22 14:11

spfi