411

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.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
UMAR-MOBITSOLUTIONS
  • 77,236
  • 95
  • 209
  • 278

19 Answers19

879

Try this

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

UPDATE

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.

Reference: https://developer.android.com/guide/topics/resources/string-resource.html

Quantum_VC
  • 185
  • 13
ccheneson
  • 49,072
  • 8
  • 63
  • 68
  • 90
    you can simplify that to `this.getString(R.string.some_id)` if you're already in a `Context` (Activity or Service). – mxk Feb 02 '10 at 16:02
  • 1
    I used this: String message += getResources().getString(R.string.string1) + "some more word..."; and I wanted to send this string via sms, but it is not working. It works fine without the string resource. Am I missing something? – keybee Mar 22 '13 at 16:47
  • 11
    You can simplify that even further to getString(R.string.some_id). And if you want to customize the string, you can make the string something like "Welcome, %1$s", then you can use getString(R.string.some_id, "John Doe"). to get "Welcome, John Doe" at runtime. – M Granja Aug 17 '13 at 21:35
  • Also works for other types like I just used `getResouces().getInteger(R.integer.my_value_in_xml)` – Noah Herron Aug 19 '15 at 00:51
66

In Activity:

this.getString(R.string.resource_name)

If not in activity but have access to context:

context.getString(R.string.resource_name)
application.getString(R.string.resource_name)
Hakan Cakirkan
  • 1,040
  • 9
  • 10
45

I'm using this:

String URL = Resources.getSystem().getString(R.string.mess_1);
Robertas Uldukis
  • 801
  • 1
  • 11
  • 20
  • 15
    Warning - according to [this answer](http://stackoverflow.com/a/8864116/3836051) quoting android's documentation, using `Resources.getSystem()` does not give you the application resources, but only android's. It should not be used for resources like `string`. I used this solution and the application crashed, throwing a `notfoundexception` that was hard to understand (as the resource exists in `strings.xml`). – et_l May 03 '17 at 10:24
26

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>
</string-array>

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"
dev4life
  • 10,785
  • 6
  • 60
  • 73
11

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.

mxk
  • 43,056
  • 28
  • 105
  • 132
Alvaro
  • 465
  • 5
  • 10
6

Solution 1

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

Solution 2

String mess = getString(R.string.mess_1)
thhVictor
  • 338
  • 6
  • 25
3

In fragments, you can use

getActivity().getString(R.id.whatever);
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
ir2pid
  • 5,604
  • 12
  • 63
  • 107
3

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

android:text="@string/NameOfTheString"

The defined text in strings.xml looks like this:

 <string name="NameOfTheString">Test string</string>
bondus
  • 119
  • 7
3

Details

  • Android Studio 3.1.4
  • Kotlin version: 1.2.60

Task

  • 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
        super.onCreate()
    }

}

Step 2. Add int extension

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

Usage

strings.xml

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

Get string value:

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

Results

enter image description here enter image description here

Vasily Bodnarchuk
  • 24,482
  • 9
  • 132
  • 127
  • Cool answer there. can you give me some advice to improve on kotlin, android. any good resources, routines that you can please recommend – mrtechmaker Sep 02 '22 at 07:35
3

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

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

and set into a variable:

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

but we can define the string into the view:

<TextView
    android:id="@+id/myTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello"/>
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
2

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

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

OR

Context mcontext=getApplicationContext();

String user=mcontext.getResources().getString(R.string.muser);
bofredo
  • 2,348
  • 6
  • 32
  • 51
Ashish Vora
  • 571
  • 1
  • 8
  • 27
1

while u write R. you are referring to the R.java 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);

Agilanbu
  • 2,747
  • 2
  • 28
  • 33
Giridhar Karnik
  • 2,213
  • 4
  • 27
  • 47
1

You can use this code:

 getText(R.string.mess_1); 

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

coderz
  • 1,366
  • 1
  • 12
  • 23
1

If you are in an activity you can use

getResources().getString(R.string.whatever_string_youWant);

If you are not in an Activity use this :

getApplicationContext.getResource().getString(R.String.Whatever_String_you_want)
Hoque MD Zahidul
  • 10,560
  • 2
  • 37
  • 40
1

If you are using Jetpack Compose, you can use

stringResource(R.string.yourstring)
Yiming
  • 321
  • 2
  • 10
0

**

I hope this code is beneficial

**

String user = getResources().getString(R.string.muser); 
dhiraj kakran
  • 467
  • 2
  • 6
  • 19
0

Update

  • 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){
    ...
    context.getString(R.string.some_id);
}

What i do is

public void someMethod(){
    ...
    App.getRes().getString(R.string.some_id)
}

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.app.Application;
import android.content.res.Resources;

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


    @Override
    public void onCreate() {
        super.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.

<application
        android:name=".App"
        ...
        >
        ...
    </application>

Now you are good to go.

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
0

getString(R.string.your_string) get the result

0

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

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