TextViewを扱う
TextView を使う。
テキストを表示する
レイアウトファイルにテキストを表示するためのView(TextView)を配置する。
<TextView
android:id="@+id/text_view"
android:text="サンプル"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
↑はサンプルだが、固定テキストの場合は原則リソースのテキストを参照する形で実装する。
リソースのテキストを参照する場合は@string/{id}で指定する。
例えばres/strings.xmlにある以下を使う場合、
<string name="app_name">SampleApplication</string>
TextViewには@string/app_nameを設定する。
<TextView
android:id="@+id/text_view"
android:text="@string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
プログラム上でテキストを設定する
プログラム上でテキストを設定することもできる。
val textView = findViewById<TextView>(R.id.text_view)
textView.text = "表示したいテキスト"
リソース(res/strings.xml)にあるテキストを参照する場合はTextView#setText(resId)を使う。
val textView = findViewById<TextView>(R.id.text_view)
textView.setText(R.string.app_name)
プログラム上で数値を表示する
数値を文字に変換する。
数値をそのままTextView.setTextに渡すと、 その数値に対応するテキストをリソースから探して表示しようとするため 正しく表示できない。
val textView = findViewById<TextView>(R.id.text_view)
val value = 3
textView.text = value.toString()
テキストを太字にする
android:textStyle="bold" を指定する。
<TextView
android:id="@+id/text_view"
android:text="サンプル"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
太字をプログラム上で設定する
TextView.setTypeface(Typeface, style) を使う。
styleにはTypeface.BOLDを指定する。
val textView = findViewById<TextView>(R.id.text_view)
textView.setTypeface(textView.typeface, Typeface.BOLD)
テキストを斜体にする
android:textStyle="italic" を指定する。
<TextView
android:id="@+id/text_view"
android:text="サンプル"
android:textStyle="italic"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
斜体をプログラム上で設定する
TextView.setTypeface(Typeface, style) を使う。
styleにはTypeface.ITALICを指定する。
val textView = findViewById<TextView>(R.id.text_view)
textView.setTypeface(textView.typeface, Typeface.ITALIC)
テキストを太字+斜体にする
android:textStyle="bold|italic"を指定する。
<TextView
android:id="@+id/text_view"
android:text="サンプル"
android:textStyle="bold|italic"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
太字+斜体をプログラム上で設定する
TextView.setTypeface(Typeface, style)を使う。
styleにはTypeface.BOLD_ITALICを指定する。
val textView = findViewById<TextView>(R.id.text_view)
textView.setTypeface(textView.typeface, Typeface.BOLD_ITALIC)
テキストの大きさ(フォントサイズ)を変える
android:textSize を指定する。
<TextView
android:id="@+id/text_view"
android:text="サンプル"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Androidでは端末の設定に文字サイズの項目があり、数段階で設定することができる。
この設定に合わせてフォントの大きさを変更してくれる単位がsp、設定に関わらず一律同じ大きさで表示する単位がdpとなる。
テキスト表示に対してはspを指定することが望ましい。
フォントサイズをプログラム上で設定する
TextView.setTextSize(size) を使う
val textView = findViewById<TextView>(R.id.text_view)
textView.setTextSize(16f) // 16sp 指定
テキストの色を変える
android:textColor を指定する。
RGB による指定と、リソースIDによる指定がある。
RGBで指定する
RGB(Reg,Green,Blue)で指定する場合は以下。
<TextView
android:id="@+id/text_view"
android:text="サンプル"
android:textColor="#FF0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
リソースIDで指定する
リソースIDで指定する場合は以下。
res/colors.xml にあらかじめ定義しておく。
<color name="text_color">#FF0000</color>
↑で定義したnameを textColorで指定する。
<TextView
android:id="@+id/text_view"
android:text="サンプル"
android:textColor="@color/text_color"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
テキストの色をプログラム上で設定する
TextView.setTextColor(color) を使う。
val textView = findViewById<TextView>(R.id.text_view)
textView.setTextColor(0xFF0000) // RGBで指定する
色指定がリソースに定義されている場合はResources.getColor(id, theme)を使って色情報を取得する。
themeはそのViewが関連するcontextが保持しているものを参照する。
val color = resources.getColor(R.color.color_name, context.theme)
textView.setTextColor(color)
テキストの位置を設定する
android:gravityで設定する。
TextViewの領域に対して表示テキストをどこに寄せて表示するかを設定でき、以下が設定できる。
centercenter_verticalcenter_horizontalstart(=left)topend(=right)bottom
TextViewの表示位置の指定は、 android:layout_gravityを利用する。こちらはViewを参照。
テキストの位置をプログラム上で設定する
setGravity(gravity)を使う。gravityはGravityクラス内に前述の設定項目が定数として定義されているので、
そちらを引数に設定する。
val textView = findViewById<TextView>(R.id.text_view)
textView.setGravity(Gravity.CENTER) // 中央寄せ指定
表示する最大行数を設定する
android:maxLinesで設定する。
その行数で収まらないテキストが設定された場合、表示できるところまで表示しそれ以降のテキストが表示されなくなる。
表示する最大行数をプログラム上で設定する
setMaxLines(num)で設定する。
長すぎるテキストを省略表示する
android:ellipsizeで設定する。最大行数などの指定によりテキストが全て表示できない場合、
テキストを省略表示(...)するよう設定する。設定できる項目は以下。
end:末尾を省略表示する(最頻出)marquee:フォーカスが当たった時にスクロールで表示される。middle:中央を省略表示するnone:省略表示しないstart:先頭を省略表示する
長すぎるテキストを省略表示をプログラム上で設定する
setEllipsize(TextUtils.TruncateAt) で設定する。
TextUtils.TruncateAtに設定項目に対応する定数が定義されているので、そちらを引数に設定する。
テキストの一部にだけ外部リンクを付ける
実装方法
以下の2つの方法がある
TextViewのautoLinkにwebを設定する方法HtmlCompat.fromHtmlを利用する方法
1. autoLink="web"を利用する方法
レイアウトXMLのTextViewに **android:autoLink="web"**を設定する。
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="詳細はこちら https://www.google.co.jp/"
android:autoLink="web" />
この設定によりhttps://...の部分が自動的にリンクになり、
ユーザーがタップすると指定されたURLのページに遷移する。
2. HtmlCompat.fromHtmlを利用する方法
「こちら」といった任意のテキストをリンクにしたい場合。 `HtmlCompat.fromHtmlを使ってHTMLタグを解釈させる。
HtmlCompat.fromHtmlにHTML文字列を渡すとSpannedオブジェクトが生成されるので、これをsetTextでテキストに設定する。
そしてmovementMethodプロパティにLinkMovementMethod()を設定することで、リンクがクリックできるようになる。
import androidx.core.text.HtmlCompat
import android.text.method.LinkMovementMethod
// ...
val textView = findViewById<TextView>(R.id.text_view)
val htmlText = "詳しい内容は<a href='https://www.google.co.jp/'>こちら</a>"
val spannedText = HtmlCompat.fromHtml(htmlText, HtmlCompat.FROM_HTML_MODE_LEGACY)
textView.text = spannedText
textView.movementMethod = LinkMovementMethod()
「こちら」の部分がリンクとして表示され、タップすると指定したURLに遷移する。
参考
テキスト周りの超細かい余白を取得する
AndroidのPaint.FontMetricsを利用して、テキストのベースラインを基準とした、厳密なピクセル単位の余白を取得できる。
これにより数ピクセル単位のズレも調整できる。
val textPaint = Paint(Paint.ANTI_ALIAS_FLAG)
textPaint.setTextSize(12)
// FontMetricsの取得
val fontMetrics : FontMetrics = textPaint.getFontMetrics()
val topY = fontMetrics.top // 最も高い場所(通常は負の値)
val ascentY = fontMetrics.ascent // 通常の最大の上昇ライン(通常は負の値)
val descentY = fontMetrics.descent // 通常の最大の下降ライン(通常は正の値)
val bottomY = fontMetrics.bottom // 最も低い場所(通常は正の値)
参考
最終更新: 2025.9.28