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

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"?>

Related

Sharing setting names and default value in PreferenceScreen and Code

I have my PreferenceSettings xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen
xmlns:app="http://schemas.android.com/apk/res-auto">
<SwitchPreferenceCompat
app:defaultValue="true"
app:key="notifications"
app:title="Enable message notifications"/>
</androidx.preference.PreferenceScreen>
To get the value in code, I write
PreferenceManager.getDefaultSharedPreferences(this).getBoolean("notifications", true)
From here, you could see that I wrote notifications twice and the default value true twice.
Is there a way to share them (e.g. in some common resources), so I could easily refactor them in without need to do in two places?
Found a way to do so
I create in values folder a file name settings.xml which contains
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="my_default">true</bool>
<string name="my_title">Enable message notifications</string>
<string name="my_key">notifications</string>
</resources>
In my Preference Setting, I just have reference to these keys
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen
xmlns:app="http://schemas.android.com/apk/res-auto">
<SwitchPreferenceCompat
app:defaultValue="#bool/my_default"
app:key="#string/my_key"
app:title="#string/my_titleg"/>
</androidx.preference.PreferenceScreen>
And my code is
PreferenceManager.getDefaultSharedPreferences(this).getBoolean(
resources.getString(R.string.my_key),
resources.getBoolean(R.bool.my_default))
With that, we could have a common place for for the key, title and default value shared by the XML and the code.

Load different colors based on conditions

I am programming an application contains layouts with some views. I load these colors from values/colors.xml. Now, I want to define multiple themes for my application, e.g. Blue and Green (some sort of blue and green colors). My question is how can I define two colors.xml file and load it based on some conditions or choosing by user. What I want is some thing like strings.xml that we can load strings based on locale defined.
Thanks in advance.
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="white">#FFFFFF</string>
</resources>
values-fr/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="white">#EEEEEE</string>
</resources>
button.setBackgroundColor(Integer.parseInt(getString(R.string.white)));

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.

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.

MonoDroid - setting background color from colors.xml throws InvalidOperationException

I have a simple layout as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/widget30"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/translucent_red"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
My colors.xml file is as follows:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="opaque_red">#f00</color>
<color name="translucent_red">#80ff0000</color>
</resources>
When I compile in VS2010 I get an error "System.InvalidOperationException: Command failed...." I have tried to place the colors.xml in res/values and res/color but I still
get the error. If I reference the color directly like:
android:background="#80ff0000"
everything works fine.
Does anyone know if the colors.xml is supported by MonoDroid and if so why I'm getting this error?
Thanks for the help!
Good you found this out. Another issue point to take into account is that you always define the color id's with lowercase letters. I used capital casing and although my c# code finds the resources with case sensitive searching, android expects lowercase id's when referring from drawable or layout xml files (using #color/the_id_here).
Figured it out, the Build Action for colors.xml was set to Content instead of AndroidResource !

Categories

Resources