XML Code Not Working - android

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>

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>

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>

Implementing a word list for use in an android application

I am developing an android application and would like to be able to create an array consisting of words from an xml file, but am unsure of how that would look. Any help is appreciated.
You can do this if you just need a preset list
In your res/values folder there should be an arrays.xml file if there isnt create it.
Then withiin the file add something like this
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="myarray">
<item>first item</item>
<item>second item</item>
<item>third item</item>
<item>foruth item</item>
</array>
</resources>
then do access it do this
String[] text = getResources().getStringArray(R.array.myarray);

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)));

Categories

Resources