How to reference custom resource-file in xml - android

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.

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.

Related

Android is ignoring /res/values-large/strings.xml

I want to use a different string resource on small screens but Android always uses the default string. Am I doing it wrong?
/res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="continue">Continue</string>
</resources>
/res/values-large/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="continue">Continue to Next Page</string>
</resources>
#string/continue always resolves to "Continue", even on large screens. It does, however use the dimensions in /res/values-large/dimens.xml so I'm sure I'm using the right resource qualifier. I also tried /res/values-w550dp/strings.xml and it didn't work either.
Am I doing something wrong or is this a limitation of Android?
values-large is only available for dimens and styles, but not for string resources.
one could possibly add custom string-arrays and then look them up accordingly.
see Android Resource Types.

Create a different directory other than res/values/styles.xml to define styles

As I asked in the title, is there any way to do so?
Now, when I put all styles into one file it looks a little crowded, I would like to separate styles.
For example:
res/values/styles_for_main_screen
res/values/styles_for_set_screen
And then in main_screen layout
<TextView
style="#styles_for_main_screen/text_view_custom_style">
</TextView>
This example obviously doesn't work, but it shows what I'd like to achieve.
I read in every tutorial that we need to put our custom styles into styles.xml file, but I wonder if there is a possibility to diversify styles in few xml files?
Every question I read was something like "how to do .... in styles.xml".
I can't find question similar to mine.
Example how it should be done, thanks #Frank N. Stein for the answer:
This how looks my custom xml res/values/styles_for_main_screen
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="custom_back">
<item name="android:background">#E81C1C</item>
<item name="android:text">whatr</item>
</style>
</resources>
and then to retrieve this style I just write:
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
style="#style/custom_back"
/>
so the convention looks like:
style="#(what I want to retrieve)/(name of style)"
from the android developers site
In XML: #[package:]style/style_name
You can call your style files whatever you want (if you respect the naming conventions and you put them all in the values folder/s), as you do with strings and colors.
Therefore, YES! You can have multiple ones, if so you desire.
Obviously, you will NOT specify the path to each file.
Referencing the style/s by using R.style.your_new_style is enough.
Remember that, android scans the files found int the /values directory by reading their content. For styles, every <style name="styleName" > ... </style> will be parser and a style object reference will be created.
Then, as Frank said, Yes. You can use whatever file name to write your custom styles.

How create resource id without a resource

I need to associate several tags to a view so I use
view.setTag(id, tag_object)
Unfortunately Android requires to have the id as defined in a resource. However R file is auto generated of resource ids appearing in different resource files, so I do not know how to create an id detached from any resource. As work around I just use id of some resource but it isn't robust, because if I decide to remove the resource, the id can disappear. It is also reduces readability of the code having some weird id for addressing a tag. Perhaps I missed very simple trick as ids resource file.
There is a resource type "id" that lets you define arbitrary resource IDs:
http://developer.android.com/guide/topics/resources/more-resources.html#Id
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="foo"/>
<item type="id" name="bar"/>
</resources>
Will generate R.id.foo and R.id.bar.
You can simply declare Strings in your strings.xml file and use those id's. For readability purpose give them some good names. And don't use these strings somewhere else in your code or resources.

Android XML include another xml (non layout)

I have a settings.xml file that I use to set various global variables within my app. I want to be able to create sub xml files that I include in the settings.xml file because depending on the client, I change settings in this file so I want to make it easier on myself.
I know that there is a way to include layout files this way but I can't find any documentation that shows how to do this with plain XML on Android.
Any help would be much appreciated.
I think all you have to do is make an additional file which includes the settings you want.
Ie, I have strings.xml in values
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="main_title">Hello there</string>
</resources>
And I can create additionalstrings.xml in the values folder
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="additional">How are you?</string>
</resources>
I can refer to either R.string.main_title or R.string.additional, without having to say the file it came from.

android - How to create xml's id?

I'm developing a dynamic keyboard application using soft keyboard sample. My application changes layout. For example I have a keyboard(has one key) then I set up the app and I can use keyboard(has one key).
I want to create an XML file after compilation and use this file in the application (reading XML file with xmlpullparser or xmlresourceparser). However, keyboard class needs XML's id. How do I create an XML id?
It can be defined in ids.xml or any custom file like constants.xml inside your res\values folder.
Examples:
<resources>
<item type="id" name="keyboard" />
</resources>
and can be accessed in the code as following:
R.id.keyboard
You can define ids using an ids.xml file in your res/values directory. Here's an example:
<resources>
<item type="id" name="my_keyboard" />
</resources>
In code, you would set the id like so:
keyboardView.setId( R.id.my_keyboard );
XML files are compiled (binary XML) and thus you don't create them at runtime.
If you want to change your keyboard layout dynamically, you'll do that programmatically rather than in XML.

Categories

Resources