Skip to content

Instantly share code, notes, and snippets.

@VladSumtsov
Created September 2, 2015 16:11
Show Gist options
  • Save VladSumtsov/28b18c248b36fe9e19ab to your computer and use it in GitHub Desktop.
Save VladSumtsov/28b18c248b36fe9e19ab to your computer and use it in GitHub Desktop.
compound drawables click listener
package com.aliens.utils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
public class OnDrawableClickListener implements View.OnTouchListener {
private static final int LEFT_DRAWABLE = 0;
private static final int TOP_DRAWABLE = 1;
private static final int RIGHT_DRAWABLE = 2;
private static final int BOTTOM_DRAWABLE = 3;
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
TextView tv = (TextView) v;
float x = event.getX();
int width = tv.getWidth();
if (x > width - tv.getCompoundDrawables()[LEFT_DRAWABLE].getBounds().width()) {
onLeftDrawableClicked(v);
} else if (x > width - tv.getCompoundDrawables()[TOP_DRAWABLE].getBounds().width()) {
onTopDrawableClicked(v);
} else if (x > width - tv.getCompoundDrawables()[RIGHT_DRAWABLE].getBounds().width()) {
onRightDrawableClicked(v);
} else if (x > width - tv.getCompoundDrawables()[BOTTOM_DRAWABLE].getBounds().width()) {
onBottomDrawableClicked(v);
}
}
return true;
}
protected void onBottomDrawableClicked(View v) {
}
protected void onTopDrawableClicked(View v) {
}
protected void onLeftDrawableClicked(View v) {
}
protected void onRightDrawableClicked(View v) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment