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
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>
This question already has answers here:
Android xml reference within xml doesn't work
(2 answers)
Closed 5 years ago.
I'm trying to set an XML attribute with a variable in the resources in Android studio with no luck.
Something like this works:
<resources>
<string name="Key">#string/Key</string>
</resources>
But something like this doesn't:
<resources>
<string name="Key" custom-attribute="#string/Key"/>
</resources>
How do you use variables in XML attributes?
This is NOT the same question as Android xml reference within xml doesn't work.
The first example works correctly, I am able to use variables setting a field value. The second example does not work, I cannot set an attribute.
<resources>
<string name="Key">Some Value</string>
</resources>
Should give you same result as:
<resources>
<string name="Key" value="Some Value"/>
</resources>
Because of that I would recommend using the first example as that works properly.
Try this:
<string name="Key1">Some Text</string>
<string name="Key2">#string/Key1</string>
update:
<string name="Key1">Some Text</string>
<string name="Key2">
<some-attribute>#string/Key1</some-attribute>
</string>
Here we can define resources in res/ folder.
You can create your own file and use below examples.
The value thing won't work.
<resources>
<string name="button">Try Again</string>
<dimen name="margin">56dp</dimen>
<bool name="isCorrect">false</bool>
<color name="background">#fff</color>
<drawable name="icon">#drawable/ic_about_us</drawable>
<integer name="count">56</integer>
<string-array name="days">
<item>Monday</item>
<item>Sunday</item>
</string-array>
</resources>
And Use by R.id.nameOfResource
Here is much more to explore about them.
Hope this helps.
Update: We can also define resources like this in build.gradle script.
android {
buildTypes.each {
it.resValue 'string', 'serverLink', "https://mylink.com"
}
}
I want to support multiple languages in my app using sqlite database and xml resources.
For example, the user can insert transaction objects. Each transaction has a category.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="categories">
<item>#string/category1</item>
<item>#string/category2</item>
<item>#string/category3</item>
<item>#string/category4</item>
<item>#string/category5</item>
</array>
</resources>
What is the correct approach to use the above values in sqlite? Should a table be created or I can use them directly from xml?
If I want the user to be able to also add his own categories, how the multi-language thing should work?
Please read the two following google guides about supporting different languages and localizing:
http://developer.android.com/training/basics/supporting-devices/languages.html
http://developer.android.com/guide/topics/resources/localization.html
the normal way is to create a strings.xml for every language:
For Storing your categories in your SQLite Database you should enumerate your Categories and store only the numbers, so you are independent of the spelling. Adding a user defined Catergory should also work this way.
MyProject/
res/
values/
strings.xml
values-es/
strings.xml
/values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">My Application</string>
<string name="hello_world">Hello World!</string>
</resources>
/values-es/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">Mi AplicaciĆ³n</string>
<string name="hello_world">Hola Mundo!</string>
</resources>
Hello I am wondering if there is a way to change a string value in my xml code without touching the .java file
e.g.
in case i have in the strings.xml
<string name="title"> This is my title</string>
If I can change inside my main.xml file
The string to have the value: This is my title
meaning to have the third word of my string on bold
Thank you
In your strings.xml file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title"> This is <b>my</b> title</string>
</resources>
I'm making an android app and since I've just started I want to try get the most organised code/resources. In my strings.xml file so far I have this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">GameController</string>
<string name="stop">Stop</string>
<string name="start">Start</string>
<string name="preferences">Preferences</string>
<string name="back">Back</string>
</resources>
All of the strings except app_name are used in an options menu. But since I will be adding much more strings I was thinking that it might be better to do something like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">GameController</string>
<string name="menu_stop">Stop</string>
<string name="menu_start">Start</string>
<string name="menu_preferences">Preferences</string>
<string name="menu_back">Back</string>
</resources>
Is it the best way or should I use another system?
It depends on where the strings will be used. If "stop" will never be used anywhere but in a menu, calling it "menu_stop" is a good idea. If it'll be used all over the place then it should just be called "stop".
Also, XML comments are very useful for organizing resources.
<resources>
<string name="app_name">GameController</string>
<!-- Menu Strings -->
<string name="menu_stop">Stop</string>
<string name="menu_start">Start</string>
<string name="menu_preferences">Preferences</string>
<string name="menu_back">Back</string>
</resources>
Finally, if you find you have tons and tons of string resources you may want to go so far as to separate them into different xml files: menu_strings.xml, dialog_strings.xml, etc.
menu_strings.xml
<resources>
<!-- Menu Strings -->
<string name="menu_stop">Stop</string>
<string name="menu_start">Start</string>
<string name="menu_preferences">Preferences</string>
<string name="menu_back">Back</string>
</resources>
dialog_strings.xml
<resources>
<string name="dialog_cancel_yes">Yes, cancel.</string>
<string name="dialog_cancel_no">No, do not cancel.</string>
</resources>
This is kind of a subjective question, really. You should use whatever you find easier to handle. I certainly do the second type of naming when I'm using layouts and drawables (e.g. button_x, ninepatch_x, icon_x, etc.), just because it keeps them next to each other, and is easier to narrow down quickly with Content Assist. In XML, you can use comments to group them together, and add white space, just anything that makes it easier for you to find what you need, and quickly.