miss_miss
miss_miss

Reputation: 41

Android: text from strings.xml displayed as @numbers

I have updated some string values in strings.xml and my application now shows not the new text but something like @234545201. I have cleaned the projected and rebuilded, there are no import android.R anywhere, just R related to my package. What went wrong?

Upvotes: 1

Views: 2261

Answers (3)

Arut
Arut

Reputation: 950

You can put your stings.xml file in the follwing format.

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Yamba</string>
<string name="titleYamba">Yamba</string>
<string name="titleStatus">Status Update</string>
<string name="hintText">Please enter your 140-character status</string>
<string name="buttonUpdate">Update</string>
</resources>

and use the name as reference of your textbox ids like.

 android:text="@string/app_name"

Upvotes: 1

Michell Bak
Michell Bak

Reputation: 13252

To obtain a string from your strings.xml file, you can do a few things.

If you need it as a String object, you can use getString(R.string.string_id) to fetch the string, given an ID.

If you're trying to set the text of, say, a TextView, you can actually simply use setText(R.string.string_id) and the OS will obtain the correct string for you.

In other words, the TextView class has a method called setText(int resid), and that's also the reason why you can't write something like the following:

TextView.setText(12345680);

Upvotes: 7

shantanu
shantanu

Reputation: 204

Are you trying to read it directly as R.string.my_string_resource? Try passing it to getString() as getString(R.string.my_string_resource).

Upvotes: 3

Related Questions