I have ListView with about 30 items. When I click on an item, some text displays.
Currently I am storing text in strings.xml, but I wonder if it's better practice to insert this text into database.
What are advantages and disadvantages of both ways? Which is faster?
If the text is static, or not changing over time, I would definitely put it in strings.xml in your res folder. All XML files in your res folder are optimized and compressed when you create an .apk of your app. A database also introduces more complexity, and you will have to connect to the database before retrieving your text. If your text changes over time, you will need to put it in a database (or plain file, if you prefer that).
If your strings which are getting displayed in list are not dynamic (not change while code excutes), I prefer having them in strings.xml rather than database, this way we can avoid unnecessary logic related to database retrieval. 30 is not a big count.
Related
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.
I've got problem with my school project.
Basically user create a table on website with records and then by android app can upload own datas to table.
Table might have many rows with different type like integer, date, text. So in my app I need to create layouts dynamicly depends on defined table. I thought that I can convert rows to xml file and then create layouts in android, but it seems to be a difficult one. Can You suggest my how can I create that layouts from downloaded xml ? Or maybe is the other simple way. Help
Whenever I make any app I always hardcode the string instead of referencing it from string resource from XML. App works fine but gives me warning to use #string resource
Example button:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click here" />
My question is whether it will affect my application performance if I do so or it(#string resource) is just used for internationalization.
This is an Android lint warning to help you with localization.
Technically, hard coding strings would make your app perform a little better, as it won't have to look up the string from the corresponding R int each time. However, this performance difference will be negligible, and no human will be able to notice it.
However, you should always keep your String resources in the values folder as it makes localization very easy.
I see no reason that you would get worse performance by using a hard-coded string. There are fewer steps involved with hard-coded strings. However, it is certainly best practice to separate resource strings from application and UI code.
It will not create any performance issue. But defining strings in strings.xml is encouraged for the ease of maintainance and lolalization. For example consider the below two scenario.
Scenario 1
When you need to change a string used in many places. In your case You will have to change all the "click here" in all layouts. But if you declared in strings.xml then only change made in the xml will change them all.
Scenario 2
For another example if you want to show different language for different locale then you need to use the string.xml.
As others said, it's for localization,
but for performance, it depends on how many times per second those strings are getting looked up.
I've seen a case where an app is slow in startup, and stack sampling showed 50% of the time was being spent in resource lookup of strings, and the reason the strings were being looked up was to display them on the splash screen that gives the user something to look at while the app is starting up!
I dont think hardcoding a string will make your program to run any slower.. Infact it will enhance the performance as there's no need for any lookup for the String in the R.java class.
Referencing the string from strings.xml is best practice due to 2 reasons:-
1- Localization
2- if you're using the same string in multiple places and would like to edit the same in all the places saves you the overhead of editing all the hard-coded strings individually.
Hard coding strings will not affect the performance directly. It affects the maintainability.
In case when you hardcode a string and in later stage if you want to change the string "Click me" to "add" or something else, then you need to search your complete project to change the string where and all it is used. So Better to follow strings.xml always. :)
Your app will not support Localization then, if that is not the requirement of your app then there will be no problem using hard coded strings.
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'm making a new application and its basically filled with information about Warcraft.
I have similar apps on my phone that have similar information and when I looked inside their .apk they only had like 10 layouts.
The app that I am making already has 5 layouts and it seems like I will need about 50-60 layouts.
So now my question is it normal to have that many layouts? Or do I have to learn to make one general layout and keep reusing it? For example, like if I need to display information about a topic for instance the classes in warcraft which are 10 different classes with 2-3 different guide pages on average for each class, would I need to make a different layout for each page or is their a better way of doing it?
I would really appreciate any input/suggestions.
What I recommend is having one layout for every type of screen (basically one per Activity) and use Java to fill in all of the info. Use getResources().getString(int id) and pass something from R.string. That means you need to keep all of your information in a strings.xml file in your values folder (located in /res/values). List all of your views in the layout XML file and then find them by ID and set their values.
It's best to keep all of your string resources in a separate XML file and not hard code them into the layout (otherwise it's a pain to replace every instance of a word you realized you misspelled or something). If you don't know how to write XML, that's okay since there are tools in Eclipse, but I HIGHLY recommend learning it.
You can re-use layouts without any problems. In fact for maintaining the code it is a very good idea to do so. No one wants to maintain 50+ layouts and associated code.