テキストを表示する(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
の領域に対して表示テキストをどこに寄せて表示するかを設定でき、以下が設定できる。
center
center_vertical
center_horizontal
start
(=left
)top
end
(=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
に設定項目に対応する定数が定義されているので、そちらを引数に設定する。
参考
最終更新: 2025.8.1