Get a string resource from “values” folder in Android

I have a default string that needs to be used on the app, no matter the language.

Thinking that it should be independent from the app language I did not put the string inside any of the string.xml files for the different languages. Instead I created another myString.xml file inside values folder, looking like that:


    <string name="myStringResource">This text shall be available
for the entire app no matter the language

But now I am not able to access this string.

Is it the wrong way to do that? And if yes, then how can I achieve the above explained scenario?


To access Strings, you do as Berat said in his answer. From code using ID’s (R.string.resName) and XML using the @string annotation: @string/resName to get the String.

However, when you use code (e.g. Java), you can’t just use the ID. You also have to use Context.getString(R.string.resName); to get the actual String. So if you want to access the String from code and get it as a variable, you do this:

String res = context.getString(R.string.myStringResource);

I write context because it is a class in Context. You can, however, use Activity instances to get the String as well. If you are writing the code inside an activity, you can just write String res = getString(R.string.myStringResource);.

Activity means either extending Activity or AppCompatActivity (or a class that extends either on some level)