Android XML include another xml (non layout) - android

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.

Related

Why there is no DTD in strings.xml and styles.xml?

I created a new android studio project and found that there is no
<?xml version="1.0" encoding="utf-8"?>
tag at the beginning of strings.xml and styles.xml but it is present in colors.xml.
Why it is not present in the strings.xml and styles.xml?
Is the absence of this tag in strings.xml and styles.xml is fine?
Is that fine to add manually into strings.xml and styles.xml files?
Thanks!!!
<?xml version="1.0" encoding="utf-8"?> is called the "XML declaration" line and it is technically optional.
Why is it not present in the strings.xml and styles.xml?
That maybe due to a technical difficulty on your android studio which can be fixed with a restart.
Is the absence of this tag in strings.xml and styles.xml fine?
The presence of it is not compulsory but the absence of it may lead to compile time error when you insert non ASCII characters to the file, like Norwegian æ ø å , or French ê è é.
Is it fine to manually add it into strings.xml and styles.xml files?
Totally yes! And you even have extra options at your disposal
<?xml version="1.0" encoding="us-ascii"?>
<?xml version="1.0" encoding="windows-1252"?>
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-16"?>

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.

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
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.

How to add localization setting in my app

Is there a way to add localization setting in my app? I'm going to add a preference/setting in my app and there is a localization option. So the user can change the language I have provided from the values string.
I was googling around but found nothing. Wondering you guys can help me, and give example or link to the tutorial.
You don't need to provide a setting for that, Android will do that for you. You just have to provide the translations for the languages you want to support.
You can do this for French for example by creating a folder named res/values-fr in your resources folder and putting your translations in there.
so in the res/values folder you would have a strings.xml file :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="q_map">Map</string>
</resources>
and in the res/values-fr you woud have another string.xml file :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="q_map">Carte</string>
</resources>
Your app will use the translation that best matches the language that the user has selected on their device.

Proper Structure for Android XML resource file

I can't figure out how to get proper XML Structure for resources in Android.
I've saved recepies.xml into values folder, and would like to pars them.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<recepies xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<recepie name="recepie1">
<item></item>
<NumberOfPersons>1</NumberOfPersons>
<Ingredients>
<string></string>
<string></string>
</Ingredients>
<Preparation></Preparation>
<Energy>342</Energy>
<Protein>8</Protein>
<RecipeCategory>3</RecipeCategory>
</recepie>
But, I get or "Invalid start tag",
if I format it without recipies tag like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?>
I get, no more pseudo attributes allowed.
If I use only this:
<?xml version="1.0" encoding="UTF-8"?>
I get that markup is not well formed.
I guess I'm missing something in basics of XML formatting, but I can't figure out what.
/res/values folder is only for android resources. You can only define one of these type of resources here.
http://developer.android.com/guide/topics/resources/available-resources.html
If you want your own xml file, place it in /assets folder.
Your resources in values.xml must look like this
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string
name="string_name">text_string</string>
</resources>
You can check the documentation on string resources or on TypedArray to get more information.

Categories

Resources