R.string.value showing up as a number
Asked Answered
S

4

8

For the below code I intended to get the system date and display it as per the formatting of the current locale, it's just that for the R.string.date. In emulator it always shows up as a long number (something like 821302314) instead of "Date: " which I has already externalized in the string.xml. Can anyone help to have a look why this is so?

final TextView mTimeText = (TextView)findViewById(R.id.mTimeText);

//get system date
Date date = new Date();
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTimeText.setText(R.string.date + " " + dateFormat.format(date));

layout.xml

<TextView 
android:id="@+id/mTimeText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"   
android:text="@string/date"
/>

strings.xml

<string name="date">Date:</string>
Syringomyelia answered 26/9, 2011 at 15:11 Comment(2)
strings.xml: <string name="date">Date:</string>Syringomyelia
You should use getString(R.string.date) in place of R.string.date #3363745Mouseear
S
15

R.string.date is indeed an int, you're missing the call to getText() or getString():

mTimeText.setText(getText(R.string.date) + " " + dateFormat.format(date));

Even better, don't build the string in your code, but use a template with getString(int resId, Object... formatArgs):

mTimeText.setText(getString(R.string.date, dateFormat.format(date)));

and in your string.xml:

<string name="date">Date: %s</string>
Selfish answered 26/9, 2011 at 15:19 Comment(2)
Thanks very much Philipp. I guess why a template is better is that it's not concatenating strings?Syringomyelia
In this case it doesn't really make a difference, but depending on the language the sentence structure might be different (French is a prime candidate here :) ). Using a template saves you from doing string acrobatics in your code to support multiple languages.Selfish
H
8

Yes, you will get the ID of the String if you use R.string.date. As stated in the docs

You can use either getString(int) or getText(int) to retrieve a string. getText(int) will retain any rich text styling applied to the string.

Example:

 this.getString(R.string.date);

Read about it here: getString

Hepler answered 26/9, 2011 at 15:19 Comment(0)
H
5

To get string value from xml, you should call this.getString(R.id.nameOfString). In your case this would be mTimeText.setText(this.getString(R.string.date) + " " + dateFormat.format(date));

Hartung answered 26/9, 2011 at 15:25 Comment(0)
C
0

To override all "R.string.*" to "getString(R.string.)"* i wrote a little regex.

This regex also ignores the strings who already have a "getString" in front.

((?!getString\() R\.string\.[a-zA-Z1-9_]+)

You just have to press Strg+Shift+R in Android Studio to open the replace terminal and insert the regex above as "Find" and as "Replacement" the regex below.

 getString\( $1 \)

Don't forget to set "regular expression" checkbox.

For me this worked perfectly. But the "Find Regex" got one problem it only finds R.string when it starts with a whitespace. I don't know how to solve this because if i delete the whitespace ill find also the R.string that allready have the "getString".

May some can help to improve the regex or has a better way to achieve this.

Ceresin answered 21/6, 2017 at 21:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.