Which one is correct about bools.xml? - android

I find there are two ways to define bool var in bools.xml, which one is correct? Thanks!
----------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="screen_small">true</bool>
<bool name="adjust_view_bounds">true</bool>
</resources>
-------------------------------------------
----------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="bool" name="api11">false</item>
</resources>
----------------------------------------

Using <bool> make code shorter and cleaner, but both syntax is correct and work.

Correct syntax for boolean would be :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool
name="bool_name"
>[true | false]</bool>
</resources>
See : http://developer.android.com/guide/topics/resources/more-resources.html

Related

Can I use another .xml file in values/strings.xml file?

I have a question about values/strings.xml
I'd like to use strings.xml, but use another strings.xml file.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello!</string>
<string name="test">veeeeeeeeeeeeeeeeeeeeeeeery long script(about 1000characters)</string>
</resources>
like this, when I want to manage veeeeery long script by another xml file.
Then can I use another .xml file in values/strings.xml file?
for example like..
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello!</string>
<string name="test"> #values/script.xml </string>
</resources>
Thank you for reading my question.
Have a nice day :)
Yes, you just need to use #string instead of #values

How to make two break ines in android string resource?

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>

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.

Android Strings with apostrophe

How to add apostrophe in Android String Resource File?
Hi!,
I'm trying to add apostrophe (') in android strings
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Stack's Answers</string>
</resources>
Try:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Stack\'s Answers</string>
</resources>
You have to escape the XML with a \ before the apostrophe because it is a character that has a special meaning in Android's XML.
Try this way..
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Stack\'s Answers</string>
</resources>

XML Code Not Working

I am a beginner at coding Android Apps.
Here is my XML code under arrays.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="difficulty">
<item>"#string/easy_label"</item>
<item>"#string/medium_label"</item>
<item>"#string/hard_label"</item>
</array>
</resources>
"#string/easy_label" and the other labels are not highlighted a color. I'm assuming my program won't run because there is a problem here. Does anyone know how I can re-adjust this to make my Android program work?
On a side note: I have easy_label, medium_label, and hard_label all declared in strings.xml, so I don't think the reference to strings.xml is the problem.
What if you do it without double quotes:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="difficulty">
<item>#string/easy_label</item>
<item>#string/medium_label</item>
<item>#string/hard_label</item>
</array>
</resources>

Categories

Resources