Strings not showing up in resource chooser - android

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.

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

Setting XML attribute with a variable [duplicate]

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

Multiple languages in android app using sqlite and xml resources

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>

How to add localization setting in my app

Is there a way to add localization setting in my app? I'm going to add a preference/setting in my app and there is a localization option. So the user can change the language I have provided from the values string.
I was googling around but found nothing. Wondering you guys can help me, and give example or link to the tutorial.
You don't need to provide a setting for that, Android will do that for you. You just have to provide the translations for the languages you want to support.
You can do this for French for example by creating a folder named res/values-fr in your resources folder and putting your translations in there.
so in the res/values folder you would have a strings.xml file :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="q_map">Map</string>
</resources>
and in the res/values-fr you woud have another string.xml file :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="q_map">Carte</string>
</resources>
Your app will use the translation that best matches the language that the user has selected on their device.

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