LoginSignup
5
2

More than 3 years have passed since last update.

TextViewのテキストに下線を引くExtension

Last updated at Posted at 2020-01-14

よく忘れるのでメモ。個人的にはハイパーリンクっぽい表現をしたいとき以外に使ったことはないです。
SpannableStringを使います。

fun TextView.drawUnderline() {
  text = SpannableString(text).apply {
    setSpan(UnderlineSpan(), 0, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
  }
}

※2020/1/15 追記
HtmlCompatを使った方が楽そう。

fun TextView.drawUnderline() {
  text = HtmlCompat.fromHtml("<u>$text</u>", HtmlCompat.FROM_HTML_MODE_COMPACT)
}

参考

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2