I have doubts regarding the following ones. please clarify it.
1. What is the problem if i set the text of text view in xml file instead of java file(String.xml)?
2. What is the usage of SP? Give me one example.can anybody helpme.
thanks
Regarding your first question: strings.xml file is used for storing string resources, especially for the sake of resolving the localization issues. You can store multiple strings.xml files inside your project in different folders depending on the localization. This makes translating your application to a different language easier: you just create another strings.xml file, translate all the strings and put it inside the corresponding folder. Hope this helps.
Related
I'm attempting to create a 'Preferences' Activity for my Wear OS app (home-baked as I don't believe the standard Settings Activity copes with round screens).
In order to support the round screen I am planning to use a WearableRecyclerView and so need to define string-arrays for the contents of the Recycler layouts.
To keep things clean in my code, I'd like to keep these string-arrays out of my strings.xml files if possible.
Therefore, is it possible to use, for example, preferences.xml in the res/values folder (and provide translations in the values-?? folders) and then reference this in code?
I have tried creating preferences.xml but when I try to retrieve the arrays with
String[] prefsTitlesArray = getResources().getStringArray(R.preferences.prefs_titles);
I get an error flagged in the IDE as 'preferences' isn't recognised under R.
Do I have to stick to the standard .xml file names such as strings.xml and array.xml or is it possible to use an arbitrary file name under the values tree to keep thinsg nice and clean and obviously named?
(Note, I have looked at Is it possible to create translateable arbitrary XML resources in Android Studio? which seems to imply that arbitrary xml file names might be possible outside of the values tree, but doesn't mention how they are referenced in code (Java, in my case).
As per Mike M's comment, yes it is possible to name the XML resource files anything you want as the code reference R.????.itemName is derived from the item type not the file it comes from.
So a file called prefs.xml could contain <string name="itemName"> items and <string-array name="itemName"> items etc and they will be referenced from code as R.string.itemName and R.array.itemName.
The XML filename itself is irrelevant so long as it is saved in the correct folder within the project for value resource files.
I'm designing an Android app that is to support 10 different languages/localities using Android Studio. The problem is that if I perfect the layout file for English it won't look right for other languages. For instance, some of the text might be truncated/cut off in another language because it is too long for the TextView, even if it is fine in English.
Is it possible to have language-based differences in font sizes in the layout (xml) file?
You should be able to create a layout-es folder to override the layout for the Spanish language etc. I have used it myself for drawables so I can't see why it wouldn't work for layouts too.
#darnmason's solution is correct -- locale-aware layout files do work, and to make it work, just duplicate the resource file in a layout folder with -xx, where xx is the language code. See the "639‑1" heading in this table for the language code.
So, for \res\layout\MainScreen.xml in French, the new file becomes \res\layout-fr\MainScreen.xml.
Despite Android Studio complaining that string resources should be localized into a (separate) resource file, it sometimes makes sense to use a duplicated locale-specific layout file; one such example is in the case referred to by the OP.
In general, try to do localization of text in a strings resource file. Sometimes, however, the translated text in one language would make the UI ugly in another language (compare length of "Nom d'utilisateur" with "用戶名", and the field sizes that result). In such cases, duplicate the layout file, as described above, as more than just the text (dimensions, perhaps fonts) is being localized.
The downside of using an additional layout file per language is that each time something changes (e.g. adding a button), multiple layout files need to be updated with all changes, rather than just adding the control to one layout file, and adding all translations to a separate strings resources file.
i did not put some text in string.xml and there is a yellow alert symbol in side of Textview field. i want to know does it have problem and what happen, if do not put texts in string.xml in android?
Well, setting your text directly on your layout file can be a source of future problems and its not recommended.
Consider this scenario: You use the same string "Foo" in many layout files and then you decided to change to "Bar". If you have hardcoded that, you will need to make changes in all layout files and there might be a chance to leave some inconsistent text. But if you place it in the strings.xml file, you will have only to change in one place.
Also, if you want to add translations to your app, the Android system can handle it automatically for you if you use the strings.xml.
You might want to take a look here and here.
Nothing will happen.
string.xml used to:
localization
res/values-fr/strings.xml
res/values-ja/strings.xml
ease of editing and to save memory when reusing.
I Recommend you to put text files in string.xml
Whilst it will not cause a problem in the short term, it is not good practice and will cause you issues in the future if you decide to support multiple languages.
Maybe this is a silly question but is there a difference,besides the obvious,between
android:text="#string/...." and android:text="..."?.I'm thinking that maybe the text that appears on the screen has the option for styling when using #string.Which one is best to use in general or it really doesn't matter?
When you're using android:text="#string/" the app is going to find the value of the string in the ressources file, with this technique you can manage multilanguage app, with a "strings"'s file by language.
Choice Matters, if your app has a lot of text in it, supporting a different language would be easier if you used #string, you would not have to scramble through every xml file to add text in the other language for every piece of text, simply go to strings resource and change there.
But then again, when checking UI for errors after work is done, it might make life harder (or work boring) as fixing a typo would require you to go to the xml file that the erroneous text is located, look up the name of string and then go to Strings to correct (unless you were very organised and named things well in your Strings such that you know which text belongs where)
I have a fairly complex android app, the contents of the 'layout' folder is becoming increasingly large. I've tried to organise the individual layout xml files into sub folders e.g. layout/dialog/, layout/activity/, layout/views/ etc. This doesn't seem to work, the content of the folders in not parsed into the R. class.
Is there a way to do this?
Thanks!
Short answer is no, subfolders are not supported. You probably just need to get clever with naming the files. See this question: Can the Android drawable directory contain subdirectories?
Resource directories should be flat. So, if your intention is to have layout/dialog, layout/activity/, layout/views/, etc. you should go with layout/dialog_whatever, layout/activity_whatever and layout/views_whatever, which gives you more or less the same organization.
No, resource directories doesn't support sub directories structures, Because it all about indexing in your R.java files,
You have to give naming conversion for your files, like, layout/activity_..
If you have more xml layout in your app then you have to give the proper naming convention as the xml use in the perticular activity.
as like if your activities are like activity1, activity2 and the xml in that activities are dialog1.xml, dialog2.xml, main1.xml, main2.xml, button1.xml, button2.xml, view1.xml, view2.xml ...etc
Then use that xml layout with naming convention as like:
layout\activity1_dialog1.xml
layout\activity1_main1.xml
layout\activity1_button1.xml
layout\activity1_view1.xml
layout\activity2_dialog2.xml
layout\activity2_main2.xml
layout\activity2_button2.xml
layout\activity2_view2.xml
Hope you got my point. It will realy help you to manage the xml layout as i am doing same thing.
Enjoy. :)