LoginSignup
0
1

More than 3 years have passed since last update.

AndroidStudioで画面に表示する文字を登録する

Last updated at Posted at 2020-05-26

AndroidStudioで画面表示に使用する文字を登録

新規アプリケーションの作成で空のアクティビティを選んでから
activity_main.xmlにボタンを追加します。

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tvMain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btMain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Button"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

この時点でコンポーネントツリーに警告が出ている。
表示内容が英語なのだが要はボタンに表示する文字 Button は、ここじゃなくて@stringリソースに書いてねということらしい。

文字列の登録

ということで@stringリソースに書いてみよう。
Stringres.png

プロジェクトのツリーから
app -> res -> values -> strings.xml
strings.xmlが@stringリソースになるので開くと

strings.xml
<resources>
    <string name="app_name">[[アプリケーションのタイトル]]</string>
</resources>

となっている。
このままだと日本語使用するとき問題になるので先頭に<?xml version="1.0" encoding="utf-8" ?>を追加する。

strings.xml
<?xml version="1.0" encoding="utf-8" ?>
<resources>
    <string name="app_name">[[アプリケーションのタイトル]]</string>
</resources>

次にボタンに表示する名称を登録します。

strings.xml
<?xml version="1.0" encoding="utf-8" ?>
<resources>
    <string name="app_name">[[アプリケーションのタイトル]]</string>
    <string name="btName">これはボタンです</string>
</resources>

今度はレイアウトのactivity_main.xmlを開いて

activity_main.xml
省略
    <Button
        android:id="@+id/btMain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="@string/btName"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

android:text=の後ろの部分を @string/btName に変更します。

これで警告は消えてボタンの名称は指定したとおりの表示になります。
TextViewのtextではなぜか警告が出ませんが、同じようにこちらも直しておきましょう。

文字列リストの登録

文字列だけでなく文字列リスト(配列)も登録出来ます。

strings.xml
<?xml version="1.0" encoding="utf-8" ?>
<resources>
    <string name="app_name">[[アプリケーションタイトル]]</string>
    <string-array name="lvEntries">
        <item>1個目</item>
        <item>2個目</item>
        <item>3個目</item>
    </string-array>
</resources>

これで文字列リスト lvEntriesが完成しました。
次に画面レイアウトactivity_main.xmlの方はボタンを消してListViewを追加します。

activity_main.xml
省略
    <ListView
        android:id="@+id/lvMain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:entries="@array/lvEntries"/>

ListViewのandroid:entriesに @array/lvEntries を指定することで文字列リストがListViewに読み込まれます。

string.xmlに書く理由

直接レイアウトファイルのactivity_main.xmlに書いても良いように思いますが、あえてstrings.xmlに書いて参照させるようにしているのは他の国の言語に変換しやすくする為なのでしょう。

複数のファイルの様々な場所に書かれている表示要素を修正するよりはstrings.xmlだけを修正する方が早いですからね。

でも表示する文字ってプログラムからの変更したりするのでは?っていうことでプログラムからも参照してみましょう。

MainActivity.kt
        val btnName = R.string.btName
        val lvEntries = R.array.lvEntrys

急にRが出てきましたがこれはリソースを参照するときの決まり文句です。
このようにすれば表示に関係する文字列を登録しておけば将来的に楽になるはずです。

上記ではリソースIDしか取得出来ないのでそれを元に実際の値を参照するには

MainActivity.kt
        val btnName = resources.getString(R.string.btName)
        val lvEntries = resources.getStringArray(R.array.lvEntrys)

このようになります。

最後に

行き当たりばったりで作る場合はいちいち使う文字列を登録していられません。
登録しても使わない場合があると思います。
どう使っていくのかはわかりませんが、まずは直に文字を書いても仕方が無い世界なのかもしれません。落ち着いてから登録している文字を採用するように変換すると楽なのかも知れません。
そう言っておいて後回しにしそうですね。

0
1
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
0
1