1

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 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

string.xml

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

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.