Last active
July 11, 2017 15:54
-
-
Save Popalay/595d6312ff3ab3978b5b9e62b85c1ca4 to your computer and use it in GitHub Desktop.
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.view.MotionEvent; | |
import android.view.View; | |
import android.widget.TextView; | |
public abstract class DrawableOnTouchListener implements View.OnTouchListener { | |
private static final int DRAWABLE_START = 0; | |
private static final int DRAWABLE_TOP = 1; | |
private static final int DRAWABLE_END = 2; | |
private static final int DRAWABLE_BOTTOM = 3; | |
@Override | |
public boolean onTouch(View view, MotionEvent event) { | |
if (event.getAction() == MotionEvent.ACTION_UP) { | |
if (!(view instanceof TextView)) { | |
throw new IllegalArgumentException("View must extends TextView"); | |
} | |
final TextView textView = (TextView) view; | |
int startOffset = view.getRight() | |
- view.getPaddingEnd() | |
- textView.getCompoundDrawables()[DRAWABLE_END].getBounds().width() | |
- textView.getCompoundDrawablePadding(); | |
int endOffset = startOffset | |
+ textView.getCompoundDrawables()[DRAWABLE_END].getBounds().width(); | |
if (event.getRawX() >= startOffset && event.getRawX() <= endOffset) { | |
return onEndDrawableTouch(event); | |
} | |
startOffset = view.getLeft() | |
+ view.getPaddingStart() | |
+ textView.getCompoundDrawablePadding(); | |
endOffset = startOffset | |
+ textView.getCompoundDrawables()[DRAWABLE_START].getBounds().width(); | |
if (event.getRawX() >= startOffset && event.getRawX() <= endOffset) { | |
return onStartDrawableTouch(event); | |
} | |
} | |
return false; | |
} | |
public boolean onStartDrawableTouch(final MotionEvent event) {return false;} | |
public boolean onEndDrawableTouch(final MotionEvent event) {return false;} | |
public boolean onTopDrawableTouch(final MotionEvent event) {return false;} | |
public boolean onBottomDrawableTouch(final MotionEvent event) {return false;} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment