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.
Related
I am trying to leave an empty line in a string resource
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">"Hello\nWorld!"</string>
</resources>
The above just puts "world" in the next line, but I need to leave an empty line. any help or sugestion would be great.
try
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">"Hello\n\nWorld!"</string>
</resources>
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"?>
In my app I have a lot of controls that should change their text color when changing drawable state. Android provides a way to do it - color state lists. For each color state list I have to create a separate XML file, like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:color="#398ede" android:state_pressed="true"/>
<item android:color="#808080"/>
</selector>
Is there any way to avoid creating a separate file for each color state list and define them all in a single file?
You must have found the answer by now, but for the sake of those who come to this link again, here's how we do it.
If you have a resource file defining multiple colors like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="xBlack">#FF000000</color>
<color name="xYellow">#FFFFFF00</color>
</resources>
You need to put this file under values (and not in colors)
To access the values you need to use:
dummy_button_id.setBackgroundColor(getResources().getColor(R.color.xBlack));
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.
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.