Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CheckBox with onTouchListener

I have a checkbox with onTouchListener, and in this listener, I have switch with these cases:

  • MotionEvent.ACTION_DOWN
  • MotionEvent.ACTION_UP
  • MotionEvent.ACTION_CANCEL

When I don't have view.performClick() in the ACTION_UP case, I get a lint warning:

onTouch should call View#performClick when a click is detected

But it causes my method to not work:

public void touchCheckBox(View view) {
    CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox);
    checkBox.performClick();
}

My method works fine if I add view.performClick() in ACTION_DOWN too.

My code for touchCheckBox() is:

public boolean onTouch(View view, MotionEvent motionEvent) {
    boolean isEventConsumed;
    switch (motionEvent.getAction()) {
        case MotionEvent.ACTION_DOWN:
            row.setBackgroundColor(mContext.getResources().getColor(R.color.blueSelection));
            isEventConsumed = true;
            break;
        case MotionEvent.ACTION_UP:
            view.performClick();
            row.setBackgroundColor(mContext.getResources().getColor(R.color.white));
            touchCheckBox(row);
            isEventConsumed = true;
            break;
        case MotionEvent.ACTION_CANCEL:
            row.setBackgroundColor(mContext.getResources().getColor(R.color.white));
            isEventConsumed = true;
            break;
        default:
            isEventConsumed = false;
            break;
    }
    return isEventConsumed;
}

});

Is view.performClick() necessary in the ACTION_UP case, or I can suppress the lint warning?

like image 489
Giks91 Avatar asked Nov 27 '25 21:11

Giks91


1 Answers

https://code.google.com/p/android-developer-preview/issues/detail?id=1394

It was reported as bug, I think you just surpress the warning.

Well, try this:

public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        // stuffs
        break;
    case MotionEvent.ACTION_UP:
        v.performClick();
        break;
    default:
        break;
    }
    return true;
}
like image 73
linhtruong Avatar answered Nov 29 '25 19:11

linhtruong



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!