Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to read value from string.xml in android?

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.

like image 449
UMAR-MOBITSOLUTIONS Avatar asked Feb 02 '10 12:02

UMAR-MOBITSOLUTIONS


People also ask

What is string xml file in Android?

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.


14 Answers

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

like image 163
ccheneson Avatar answered Oct 19 '22 15:10

ccheneson


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)
like image 26
Hakan Cakirkan Avatar answered Oct 19 '22 15:10

Hakan Cakirkan


I'm using this:

String URL = Resources.getSystem().getString(R.string.mess_1);
like image 45
Robertas Uldukis Avatar answered Oct 19 '22 14:10

Robertas Uldukis


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"
like image 39
oabarca Avatar answered Oct 19 '22 15:10

oabarca


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.

like image 39
Alvaro Avatar answered Oct 19 '22 16:10

Alvaro


Solution 1

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

Solution 2

String mess = getString(R.string.mess_1)
like image 28
thhVictor Avatar answered Oct 19 '22 16:10

thhVictor


In fragments, you can use

getActivity().getString(R.id.whatever);
like image 31
ir2pid Avatar answered Oct 19 '22 14:10

ir2pid


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>
like image 37
bondus Avatar answered Oct 19 '22 14:10

bondus


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);
like image 44
Ashish Vora Avatar answered Oct 19 '22 15:10

Ashish Vora


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

like image 26
Vasily Bodnarchuk Avatar answered Oct 19 '22 16:10

Vasily Bodnarchuk


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.

like image 30
coderz Avatar answered Oct 19 '22 14:10

coderz


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)
like image 26
Hoque MD Zahidul Avatar answered Oct 19 '22 14:10

Hoque MD Zahidul


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);

like image 34
Giridhar Karnik Avatar answered Oct 19 '22 15:10

Giridhar Karnik


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"/>
like image 27
Jorgesys Avatar answered Oct 19 '22 15:10

Jorgesys