Skip to content

Instantly share code, notes, and snippets.

@hector6872
Last active November 27, 2017 13:10
Show Gist options
  • Save hector6872/516ea7e04c8be4c55d26 to your computer and use it in GitHub Desktop.
Save hector6872/516ea7e04c8be4c55d26 to your computer and use it in GitHub Desktop.
UnderlineTextView (Java & Kotlin)
public class UnderlineTextView extends TextView {
private Context context;
private int textColor;
public UnderlineTextView(Context context) {
this(context, null);
}
public UnderlineTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public UnderlineTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public UnderlineTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context);
}
private void init(Context context) {
this.context = context;
}
@Override public void setText(CharSequence text, BufferType type) {
SpannableString spannableString = new SpannableString(text);
spannableString.setSpan(new UnderlineSpan(), 0, spannableString.length(), 0);
super.setText(spannableString, type);
}
@SuppressLint("InlinedApi") @Override public boolean onTouchEvent(MotionEvent event) {
if (isClickable()) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
textColor = getCurrentTextColor();
setTextColor(
getColorFromTheme(context, android.R.attr.colorAccent, lightenColor(textColor, 0.5f)));
} else if (event.getAction() == MotionEvent.ACTION_UP) {
setTextColor(textColor);
}
}
return super.onTouchEvent(event);
}
private int getColorFromTheme(Context context, int id, int defaultValue) {
TypedValue value = new TypedValue();
try {
Resources.Theme theme = context.getTheme();
if (theme != null && theme.resolveAttribute(id, value, true)) {
if (value.type >= TypedValue.TYPE_FIRST_INT && value.type <= TypedValue.TYPE_LAST_INT) {
return value.data;
} else if (value.type == TypedValue.TYPE_STRING) {
return ContextCompat.getColor(context, value.resourceId);
}
}
} catch (Exception ignored) {
}
return defaultValue;
}
private int lightenColor(int color, float factor) {
int red = (int) ((Color.red(color) * (1 - factor) / 255 + factor) * 255);
int green = (int) ((Color.green(color) * (1 - factor) / 255 + factor) * 255);
int blue = (int) ((Color.blue(color) * (1 - factor) / 255 + factor) * 255);
return Color.argb(Color.alpha(color), red, green, blue);
}
}
class UnderlineTextView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : AppCompatTextView(
context, attrs, defStyleAttr) {
private val originalTextColor = textColors.defaultColor
override fun setText(text: CharSequence, type: TextView.BufferType) {
val spannableString = SpannableString(text)
spannableString.setSpan(UnderlineSpan(), 0, spannableString.length, 0)
super.setText(spannableString, type)
}
override fun onTouchEvent(event: MotionEvent): Boolean {
if (hasOnClickListeners()) when (event.action) {
MotionEvent.ACTION_DOWN -> setTextColor(highlightColor)
MotionEvent.ACTION_UP -> {
setTextColor(originalTextColor)
return performClick()
}
}
return super.onTouchEvent(event)
}
override fun performClick(): Boolean {
super.performClick()
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment