Using the android:text="@string" does not replace string value in the strings.xml file

I am new to Android development. I recently downloaded the latest Android Studio 3.0.1. I am using Wallace Jackson "Android Apps for Absolute Beginners". I have 2 XML files when I created my (first) application per the book:

  1. activity_main.xml
  2. strings.xml

activity_main.xml file has hard coded the android:txt="Hello World!" The book guides you to modify file 1 to refer to strings.xml for this string. File 2, strings.xml, is modified to add a 2nd line containing the string that is modified slightly by removing the "!".

When I look at the preview layout pane in the Android SDK I don't see the string "Hello World"

Instead, I see "@string".

I am sure I doing something basically wrong, or missing a crucial step, or steps.

This is the activity_main.xml file (file 1)

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.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="com.example.michael.myapplication.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    </android.support.constraint.ConstraintLayout>

This is strings.xml file (file 2):

    <resources>
        <string name="app_name">My Application</string>
        <string name="app_message">Hello WORLD</string>
    </resources>

3 answers

  • answered 2018-02-13 01:31 Modge

    You have to specify the string name

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_message"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    

    To access a resource on android you have to provide its resource ID to be able to access it.

    See: https://developer.android.com/guide/topics/resources/accessing-resources.html

  • answered 2018-02-13 01:31 yatin deokar

    Access String recourses like android:text="@string/string_name"

    string.xml

    <string name="string_name">hi this is my string</string>
    

  • answered 2018-02-13 01:31 Android Geek

    In your strings.xml

    <string name="app_message">Hello WORLD</string>
    

    In your layout

    <?xml version="1.0" encoding="utf-8"?>
        <android.support.constraint.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="com.example.michael.myapplication.MainActivity">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_message"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        </android.support.constraint.ConstraintLayout>