Setting XML attribute with a variable [duplicate] - android

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

Related

How to make modules/library resources private android

I do according to documentation
module common created
res/values/public.xml
<resources>
<public name="app_name" type="string"/>
</resources>
Also tried
<resources>
<public/>
</resources>
res/values/strings.xml
<resources>
<string name="app_name">app_name</string>
<string name="app_name_2">app_name_2</string>
</resources>
Also tried without public.xml
res/values/strings.xml
<resources>
<public name="app_name" type="string"/>
<string name="app_name">app_name</string>
<string name="app_name_2">app_name_2</string>
</resources>
<resources>
<public />
<string name="app_name">app_name</string>
<string name="app_name_2">app_name_2</string>
</resources>
module app can use private resources
android.example.common.R.string.app_name_2
I think the problem is from Android Developers side, because their video says how private resources should work, but they don't work that.
That's why I created an IssueTracker request, everyone who is affected can vote (upper right corner) so that Google pays attention to the error.

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

JavaDoc for Android XML (string) resources

Is there a way how to add comment/description/documentation to android resource reference? The only way I know is to use standart XML comment. Which is not ideal obviously.
Something like special attribute or special javadoc pre-element
<resources>
<string documentation="Some useful information what does this resource means..." name="KEY" translatable="false">value</string>
</resources>
<resources>
<documentation forName="KEY">Some useful information what does this resource means...</documentation>
<string name="KEY" translatable="false">value</string>
</resources>
Not ideal but currently functional way:
<resources>
<!-- Some useful information about what this resource means... -->
<string name="KEY" translatable="false">value</string>
</resources>
The following format allows for a file header (using XML documentation comments), as well as as an individual comment (a documentation attribute) that goes along with each resource entry. I haven't had the compiler complain that a specific namespace (other than xmlns:tools) that contains documentation is required.
<?xml version="1.0" encoding="utf-8"?>
<!--=============================================================================================
My File description here
==============================================================================================-->
<resources xmlns:tools="http://schemas.android.com/tools">
<string name="Application_Name" documentation="Name of the application. Used as Activity Name as the title, and appears in About box">FantasticApp</string>
</resources>

Strings not showing up in resource chooser

Hi, I'm also doing the same tutorial
http://www.vogella.de/articles/Android/article.html#first_uiproperties.
I've managed to add strings to my strings.xml and it shows up calculate but when I go to resource chooser and try to add it to the button its not there. So what I did was create another string called time and then went back into my graphical layout and try to add it to a button and that doesn't show up. I've saved the project but that doesnt help I've also tried adding several strings but none of them show up in my resources chooser.
Any help would be much appreciated.
Here is my strings.xml file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">TestApp</string>
<string name="fahrenheit">to Fahrenheit</string>
<string name="action_settings">Settings</string>
<color name="myColor">#F5F5F5</color>
<string name="hello_world">Hello world!</string>
<string name="celsius">to Celsius</string>
<string name="calculate">to Calculate</string>
</resources>
Remove the colour
check if u have imported android.R if u have imported in the change the import to your R file.
clean the project.

Organizing Strings.xml

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.

Categories

Resources