2
votes

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

Edit: It turns out that @string does NOT specifically reference strings.xml. I was originally under the impression that it did and knowing that know makes my question seem a tad silly.

1
The question is not at all clear. Are you talking about a file like a layout file or just the data ?Gaurav Sarma
@gaurav4sarma a new file similar to strings.xmlKris

1 Answers

5
votes

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.