how to read value from string.xml in android?

The question:

I have written the line:

String Mess = R.string.mess_1 ;

to get string value, but instead of returning string, it is giving me id of type integer. How can I get its string value? I mentioned the string value in the string.xml file.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Try this

String mess = getResources().getString(R.string.mess_1);


String string = getString(R.string.hello);

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.


Method 2

In Activity:


If not in activity but have access to context:


Method 3

I’m using this:

String URL = Resources.getSystem().getString(R.string.mess_1);

Method 4

By the way, it is also possible to create string arrays in the strings.xml like so:

<string-array name="tabs_names"> 
    <item>My Tab 1</item> 
    <item>My Tab 2</item>

And then from your Activity you can get the reference like so:

String[] tab_names = getResources().getStringArray(R.array.tab_names);
String tabname1=tab_names[0];//"My Tab 1"

Method 5

Only for future references.

In the String resources documentation it says:

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.

Method 6

Solution 1

Context context;
String mess = context.getString(R.string.mess_1)

Solution 2

String mess = getString(R.string.mess_1)

Method 7

In fragments, you can use


Method 8

If you want to add the string value to a button for example, simple use


The defined text in strings.xml looks like this:

 <string name="NameOfTheString">Test string</string>

Method 9

You must reference Context name before using getResources() in Android.

String user=getApplicationContext().getResources().getString(R.string.muser);


Context mcontext=getApplicationContext();

String user=mcontext.getResources().getString(R.string.muser);

Method 10

You can use this code:


Basically, you need to pass the resource id as a parameter to the getText() method.

Method 11

If you are in an activity you can use


If you are not in an Activity
use this :


Method 12


  • Android Studio 3.1.4
  • Kotlin version: 1.2.60


  • single line use
  • minimum code
  • use suggestions from the compiler

Step 1. Application()

Get link to the context of you application

class MY_APPLICATION_NAME: Application() {

    companion object {
        private lateinit var instance: MY_APPLICATION_NAME

        fun getAppContext(): Context = instance.applicationContext

    override fun onCreate() {
        instance = this


Step 2. Add int extension

inline fun Int.toLocalizedString(): String = MY_APPLICATION_NAME.getAppContext().resources.getString(this)



    <!-- .......  -->
    <string name="no_internet_connection">No internet connection</string>
    <!-- .......  -->

Get string value:

val errorMessage = R.string.no_internet_connection.toLocalizedString()


how to read value from string.xml in android?
enter image description here

Method 13

while u write R. you are referring to the class created by eclipse, use getResources().getString() and pass the id of the resource from which you are trying to read inside the getString() method.

Example : String[] yourStringArray = getResources().getStringArray(R.array.Your_array);

Method 14

You can read directly the value defined into strings.xml:

    <string name="hello">Hello StackOverflow!</string>

and set into a variable:

String mymessage = getString(R.string.hello);

but we can define the string into the view:


Method 15


I hope this code is beneficial


String user = getResources().getString(R.string.muser); 

Method 16


  • You can use getString(R.string.some_string_id) in both Activity or Fragment.
  • You can use Context.getString(R.string.some_string_id) where you don’t have direct access to getString() method. Like Dialog.

Problem is where you don’t have Context access, like a method in your Util class.

Assume below method without Context.

public void someMethod(){
    // can't use getResource() or getString() without Context.

Now you will pass Context as a parameter in this method and use getString().

public void someMethod(Context context){

What i do is

public void someMethod(){

What? It is very simple to use anywhere in your app!

So here is a Bonus unique solution by which you can access resources from anywhere like Util class .

import android.content.res.Resources;

public class App extends Application {
    private static App mInstance;
    private static Resources res;

    public void onCreate() {
        mInstance = this;
        res = getResources();

    public static App getInstance() {
        return mInstance;

    public static Resources getResourses() {
        return res;


Add name field to your manifest.xml <application tag.


Now you are good to go.

Method 17

getString(R.string.your_string) get the result

Method 18

String myString = getResources().getString(R.string.here_your_string_name);

Now your string is copied into myString. I hope it will work for you.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment