Skip to content

Instantly share code, notes, and snippets.

@Popalay
Last active July 11, 2017 15:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Popalay/595d6312ff3ab3978b5b9e62b85c1ca4 to your computer and use it in GitHub Desktop.
Save Popalay/595d6312ff3ab3978b5b9e62b85c1ca4 to your computer and use it in GitHub Desktop.
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