user2990508
user2990508

Reputation: 255

Problems with Strings from String.xml and integers

I'm a beginner Android Developer My program compiles, but one line doesn't work properly.

Here is the line:

String m = R.string.mess2 + time/1000 + R.string.mess3;

The variable "time" is an int

And here is the Strings.xml file from where R.string.mess2 and R.string.mess3 are retrieved

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="mess2">Amazing, you read the End User License Agreement in </string>
   <string name="mess3"> seconds, go back and read it!</string>        
   </resources>

When the app is launched, where the strings should be takes place really long decimal.

This would usually work with As3 or C++, but it doesn't work here.

And one more thing, according to these responses, you make an array like this:

String[] MyString;

So the [ ] is placed in the variable type instead of the name?

Upvotes: 0

Views: 173

Answers (5)

Allen Kusiluka
Allen Kusiluka

Reputation: 1

It's a good practice to use getResources in android, sometimes you can ignore the warning and it will work just fine.

Upvotes: 0

Siddharth_Vyas
Siddharth_Vyas

Reputation: 10100

You need to use :

YourActivity.this.getString(R.string.mess2)+time/1000 + YourActivity.this.getString(R.string.mess2);

Hope this helps.

Upvotes: 0

GeorgeChen
GeorgeChen

Reputation: 333

You might try

<string name="mess">Amazing, you read the End User License Agreement in %d seconds, go back and read it!</string>    

And java code will be

String m = String.format(context.getResources().getString(R.string.mess), time/1000);

Upvotes: 1

NTC
NTC

Reputation: 49

R.string.mess2 is ResourceId.

You should be using

getString(R.string.mess2) to get string from string.xml in Android.

Upvotes: 1

Szymon
Szymon

Reputation: 43023

You should be using the following code to get resources:

getResources().getString(R.string.someid)

Otherwise you're using the integer identifier of the resource, not the string from xml.

So in your case it should be

String m = getResources().getString(R.string.mess2) + time/1000 + getResources().getString(R.string.mess3);

Upvotes: 2

Related Questions