LoginSignup
1
1

More than 1 year has passed since last update.

Android開発でハードコーディングすると怒られるのでstrings.xmlを使う

Posted at

レイアウトファイルのactivity_main.xmlで以下のようなハードコードをした場合にAndroidStudioやIDEAで怒られます。


android:text="クリック"

strings.xml

<resources>
    <string name="app_name">addApp</string>
    <string name="btnName">クリック</string> // 追加
</resources>

strings.xmlに上記のように記述すると、@string/btnName
というような書き方で定義した文字列を取得することができます。

    <Button
            // android:text="クリック" 削除
            android:text="@string/btnName"  // 追加
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:id="@+id/btnAdd"
            app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/ed2" android:layout_marginTop="8dp"/>

これでワーニングが消えました。

スクリーンショット 2021-11-06 18.08.07.png

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