Kris Campos Kris Campos - 3 months ago 10
Android Question

How to reference custom resource-file in xml

How does you register a custom xml-resource-file with android studio so that you can reference it from other xml files?

I understand that its possible to call values from new resource values programmatically using R but I was wondering if there was a way to do that statically from another XML file.

For example, referencing that new file (in this case called keys.xml) in another xml file

<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@keys/MAPS_API_KEY" />


as opposed to using strings.xml

Answer

Just create a resource file like:

keys.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="testKey">TEST_VALUE</string>
</resources>

In code, you can call:

getString(R.string.testKey);

From another xml:

<TextView
...
android:text="@string/testKey"
...
/>

The trick is you declare as a <resources> in your xml file.

Comments