LoginSignup
4
2

More than 1 year has passed since last update.

[Android] [Kotlin] TextViewから文字がはみ出しても大丈夫!TextViewの自動スクロール方法

Last updated at Posted at 2023-02-16

できたこと

TextViewから文字がはみ出して、2行になったり文字が切れたりすることあると思います。
そんな時にTextViewが自動でスクロールして、はみ出しても全文字表示させる方法です。
XMLのTextViewに
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
を加え、ソースコードで
isSelected = true
にすると自動スクロールしてくれます。
全TextViewで「isSelected = true」をするのはコードも多くなると思うので、
TextViewが含まれているLayoutで「isSelected = true」しても良いかと思います。
ただその時の注意点の例として、
Buttonで実装した時、「state_selected="true" or "false"」などで
Buttonの背景色を変えようとしていた場合、isSelectedがずっとtrue状態になってしまうので、気をつけた方が良いです。
(↑私がそうなりました。)
自動でスクロールしてくれるってなんかいいですよね!w
うまく実装して見やすいアプリを作っていきましょう。

.xml
<TextView
    android:id="@+id/tv"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:singleLine="true"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever" />
.kt
findViewById<TextView>(R.id.tv).isSelected = true
4
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
4
2