Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference string resource from code

I have the following string declared in strings.xml:

<string name="last_msg">Your last click was on</string>

Now when someone clicks a button, I want a textview to show this string, with a space, then a variable value that is a timestamp.

Unfortunately, using @string/last_msg isn't working, and I'm not sure how to do this properly so I'm not hardcoding in content.

Here's my code for the onClick function:

public void showMsgNow(View view) {
    TextView lastMsg = (TextView)findViewById(R.id.textView2);
    long currentTimeStamp = System.currentTimeMillis();
    lastMsg.setText(@string/last_msg + " " + currentTimeStamp);
}

I'm a newbie, any help would be great !

like image 954
scarhand Avatar asked May 22 '12 09:05

scarhand


People also ask

How do we define a String resource?

A string resource provides text strings for your application with optional text styling and formatting. There are three types of resources that can provide your application with strings: String. XML resource that provides a single string.

Why should you use String resources instead of hard coded strings in your apps?

It allows you to easily locate text in your app and later have it translated. Strings can be internationalized easily, allowing your application to support multiple languages with a single application package file (APK).

What is strings XML?

String. xml file contains all the strings which will be used frequently in Android project. String. xml file present in the values folder which is sub folder of res folder in project structure.In Android Studio, we have many Views such as TextView,Button,EditText,CheckBox,RadioButton etc.


1 Answers

I found the answer on Google:

getString(R.string.last_msg)
like image 118
scarhand Avatar answered Nov 08 '22 18:11

scarhand