-
-
Save bogdanRada/ac2e22715d4bff32418be818dc60a5b9 to your computer and use it in GitHub Desktop.
Small TextView extension to set it's textStyle to bold when its clicked.
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
public class ClickTextView extends android.support.v7.widget.AppCompatTextView { | |
Rect rect; | |
public ClickTextView(Context context) { | |
super(context); | |
init(); | |
} | |
public ClickTextView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
} | |
public ClickTextView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(); | |
} | |
private void init(){ | |
setClickable(true); | |
rect = new Rect(); | |
} | |
@Override | |
public boolean onTouchEvent(MotionEvent event) { | |
int action = event.getAction(); | |
if (action == MotionEvent.ACTION_DOWN | |
|| action == MotionEvent.ACTION_HOVER_ENTER){ | |
this.setTypeface(Typeface.DEFAULT_BOLD); | |
getHitRect(rect); | |
}else if (action == MotionEvent.ACTION_MOVE){ | |
boolean inView = rect.contains(getLeft() + (int) event.getX(), getTop() + (int) event.getY()); | |
this.setTypeface(inView ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT); | |
}else if (action == MotionEvent.ACTION_UP | |
|| action == MotionEvent.ACTION_CANCEL | |
|| action == MotionEvent.ACTION_HOVER_EXIT | |
|| action == MotionEvent.ACTION_OUTSIDE) { | |
this.setTypeface(Typeface.DEFAULT); | |
} | |
return super.onTouchEvent(event); | |
} | |
@Override | |
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { | |
super.onFocusChanged(focused, direction, previouslyFocusedRect); | |
if (focused) this.setTypeface(Typeface.DEFAULT_BOLD); | |
else this.setTypeface(Typeface.DEFAULT); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment