Giks91
Giks91

Reputation: 301

CheckBox with onTouchListener

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

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?

Upvotes: 3

Views: 915

Answers (1)

linhtruong
linhtruong

Reputation: 441

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;
}

Upvotes: 2

Related Questions