Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference a string in strings.xml of an Android library in code?

To reference string foo in strings.xml (in res\values) of an app project, one can simply use

getString(R.string.foo)

getString is a method of Context.

Suppose an Android library has a string foo in its strings.xml. How can it be used in a method of the library?

Edited: It has been suggested to pass a reference of Context to the library method so getString() can be used. Since it is a context of an app's project, there is potential conflict that can be illustrated as following:

Suppose: The library has a string foo with value = "library foo". An app project has a string foo with value = "app foo"

The following code

Log.d("Debug", "App foo ID: " + R.string.foo);  
Log.d("Debug", "App: foo value: " + getString(R.string.foo));

generates:

03-22 05:53:55.590: D/Debug(16719): App foo ID: 2131230723
03-22 05:53:55.590: D/Debug(16719): App foo value: app foo

In a library method, the following code

Log.d("Debug", "Library foo ID: " + R.string.foo); 
Log.d("Debug", "Library foo value: " + context.getString(com.my.library.R.string.foo));

generates:

03-22 05:55:03.680: D/Debug(16719): Library foo ID: 2131230723
03-22 05:55:03.680: D/Debug(16719): Library foo value: app foo

The above shows the ID conflict hence erroneous string value.

like image 333
Hong Avatar asked Mar 21 '13 20:03

Hong


People also ask

Where is strings xml 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.

Which tells Android to look up a text value from the string resource file?

Let's start with the first part, @string . This is just a way of telling Android to look up a text value from a string resource file. In our case, Android Studio created a string resource file for us called strings.

What is a string in xml?

Note: A string is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). So, you can combine string resources with other simple resources in the one XML file, under one <resources> element. file location: res/values/filename.xml.

What is the use of string xml file in Android?

A string is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). So, you can combine string resources with other simple resources in the one XML file, under one <resources> element.


1 Answers

//For boolean values
    int resId = context.getResources().getIdentifier("bool_ID", "bool", context.getPackageName());
    if(resId != 0){
       System.out.println(context.getResources().getBoolean(resId));
    }

    //For String values
    int resStringId = context.getResources().getIdentifier("string_ID", "string", context.getPackageName());
    if(resStringId != 0){
        System.out.println(context.getResources().getString(resStringId));
    }
like image 91
Anand M Joseph Avatar answered Oct 03 '22 00:10

Anand M Joseph