Last active
January 18, 2021 09:29
-
-
Save Sottti/482059eb00e74030800e94d7162d2766 to your computer and use it in GitHub Desktop.
Handles clicks on a EditText right drawable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[...] | |
mEditText.setOnTouchListener( | |
new OnEditTextRightDrawableTouchListener(mEditText) { | |
@Override | |
public void OnDrawableClick() { | |
// The right drawable was clicked. Your action goes here. | |
} | |
}); | |
[...] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.graphics.drawable.Drawable; | |
import android.support.annotation.NonNull; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import android.view.View.OnTouchListener; | |
import android.widget.EditText; | |
public abstract class OnEditTextRightDrawableTouchListener implements OnTouchListener { | |
private final EditText mEditText; | |
public OnEditTextRightDrawableTouchListener(@NonNull final EditText editText) { | |
mEditText = editText; | |
} | |
@Override | |
public boolean onTouch(View view, MotionEvent motionEvent) { | |
if (motionEvent.getAction() == MotionEvent.ACTION_UP) { | |
final int DRAWABLE_RIGHT_POSITION = 2; | |
final Drawable drawable = mEditText.getCompoundDrawables()[DRAWABLE_RIGHT_POSITION]; | |
if (drawable != null) { | |
final float touchEventX = motionEvent.getX(); | |
final int touchAreaRight = mEditText.getRight(); | |
final int touchAreaLeft = touchAreaRight - drawable.getBounds().width(); | |
if (touchEventX >= touchAreaLeft && touchEventX <= touchAreaRight) { | |
view.performClick(); | |
OnDrawableClick(); | |
} | |
return true; | |
} | |
} | |
return false; | |
} | |
public abstract void OnDrawableClick(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment