I'm wondering if I should use a resource XML file for to store all the possible recipes for me rather than using an ArrayList that is hardcoded, the problem is that I don't know how to call from a resource file using the method i have...
Here is a cut down version of what I want:
int recipeNumber = b.getInt("RECIPE"); //This is taken from another activity
final TextView rowTextView = new TextView(this); //Create a textview
rowTextView.setText(R.string.recipeNumber); //This is what i am struggling with
howToLinearLayout.addView(rowTextView); //Add textview to linearlayout
I don't know what to put in the part that references my resource file. I know I need:
rowTextView.setText(R. but I'm not sure what would come after that.
Im storing strings that would be for example:
<string name="1">One part Vodka, One part Coke</string>
<string name="2">One part Vodka, One part Lemonade</string>
This list will be quite long so id appreciate any other suggestions on storage, bare in mind I'm pretty new to this.
The recipeNumber int is what string will be called in to the textview.
Thanks for any help
I recommend storing the values outside your code, for example in XML or JSON (quicker and easier to work with). I would go with JSON. Really easy to load a list into an array, or other collection, and work with it anyway you want.
http://www.vogella.com/articles/AndroidJSON/article.html
You can then maintain your recipes, add new ones, maintain it etc without having to touch the code. Simply replace the XML or JSON (or whatever you choose) and rebuild your app.
Strings.xml is intended for pieces of text in your UI and code which you might want to localise and to avoid hard coding strings. It's not really intended for storing data.
To get string resources in code, use getResources().
rowTextView.setText(getResources().getString(R.string.recipeNumber));
To get an unknown resource identifier, but a known resource name, you can use the following method.
int identifier = getResources().getIdentifier("" + recipeNumber, "string", "com.your.package.name");
rowTextView.setText(getResources().getString(identifier));
Related
In android, we can store string values either in strings.xml file or in some constants class as static final variable.Is there some reason for selecting one over another in some circumstances?
In a nutshell:
value for use in code: use always constants class.Advantage: codes remain integrated and your package can be utilized in other projects/contexts. You can not do that with string.xml as it is not transported with your package.
value for display in UI: use string.xml. Advantage: you can use localization to display translated texts.
Some situation may arise when both option appears viable. You will have to then decide where are its related values are stored.
As a general rule, use the strings.xml because android uses that XML to enable translating your app into different languages, which it can't do with strings that are hardcoded.
The official android guide on localization say the following;
Move all strings into strings.xml
As you build your apps, remember not to hard code any string. Instead
declare all of your strings as resources in a default strings.xml file
which makes it easy to update and localize. Strings in strings.xml
file can be extracted, translated and integrated back into your app
(with appropriate qualifiers) without any changes to compiled code.
If you generate images with text, put those strings in strings.xml as
well, and regenerate the images after translation.
Strings that are not going to be displayed to the user in any way needn't be stored in the XML, because they will never need translating, and you probably don't want the android system tampering with them in ways you might not know about during runtime.
If the string value is used to display in UI store in Strings.xml Otherwise keep it in code. There can be JSONTags, Key for different api/Thirdparty libraries.These kind of things should be kept in code itself.
strings.xml it is used for localization and needs a context to retrieve the content of a String. If you need a java constant to be accessed in different classes, you a public static final String member. If the string is a message for the user you should use strings.xml
If strings represent text readable by user, and which could potentially be translated to other languages (names of buttons, labels, notification/error messages, etc.) then they should be in strings.xml (actually, it can be any file name you like, not just "strings").
If string is some constant which is used in the app internally (bundle/intent keys, fragments tags, etc.) they should be declared in class
It depends, if it is a text string that will be translated or displayed to the user then for 118n sake, you will want to put in into strings.xml.
However, if the string is something like a server url or api code then you'll want to store those in code as a public static final String
I am new to android, I am trying to create a sample UI which is described in the developer.android website. When I add a edit text, I am getting that error message.
If you are currently using this: android:hint = "#string/edit_message", just do like that: android:hint = "edit_message". This is called hardcoded string. Its working, but its not recommended that you make strings this way. The other way is to go to resources, then to strings folder, and there should be one .xml, with strings. To make a new string, just copy the last one, change the name, and the content between >...<, where your string should be. To use that string you get it by its name. So if you name it "myStr", to use it in editText, you need to say: android:hint="#string/myStr" and you will get the content of the string. Hope this helps. Mark as answer if it does. Good luck.
Did you add the string resource?? To the left is package explorer. Select Your project and go to its res folder. In res folder select values and then click on strings.xml. Add your string resource there and save it. Then I dont think theres scope for such error messages.
can anybody help me ?? I want to add String value through coding in string.xml
I am doing this.
String name = getResources().getString(R.string.name);
if(name.lenght() < 1 ){
// getResources().setString(R.string.name);??????????????????????
}
My string.xml is
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="name"></string>
</resources>
does anybody know how i can add value of name in string.xml though coding.
Thank you!!
The resources are pretty much set in stone, so you can't modify them at runtime. If you need to store some new strings, use SharedPreferences or SQLite.
I don't think you want to do that. If you are trying to store a value in a persistent way, take a look at SharedPrefences. Google has a good introduction to it here.
It is not possible to modify the resources of an APK during runtime.
You can't edit those resources directly. You might want to look into sharedpreferences http://developer.android.com/reference/android/content/SharedPreferences.html or creating your own xml file.
You cant edit a resource or add a resource once the code is compiled. I dont know exactly what setResource does, but once your program is compiled, android builds the gen files which designate a certain amount of space for those variables, changing the variable once written would cause overflow or outofbounds errors with memory. If you want persistent values try using the SharedPrefs, SQL or even your own XML stored within the directory of the app, which you could set to only be readable by your app.
Hey, I have a lot of Strings that I use into my app, the .txt file that I use has ~14000 lines.. and each 3-10 lines are divided into sections like <String="Chapter I"> ... </String> ..
Speaking of performance/speed, should I put the sections into a Database, Or read line by line through the .txt file and check if the section number is the current one? Will this affect speed/performance?
I could also divide each ~2000 lines into a different .txt file so there would be less lines to go through. Is this a bad way of storing data? Thanks
I think sqlite would do the trick. It will probably be way faster than parsing a text file, plus you wont have to maintain the headache of your own ad hoc text database, or build a parser in the first place. Basically, use it, its way easier.
The standard way to deal with Strings in Android is to put them into res/values/strings.xml (I'm pretty sure you can have multiple String files in that directory if you like). If you are developing in Eclipse it will automatically populate the R class (the resource class) with constants that you can use to reference these Strings in your code:
R.string.mystring
Or in XML layouts:
#string/mystring
Or if you're doing something more custom you can use:
String string = getString(R.string.hello);
I would definitely choose this over a .txt file. It's much easier. All the work is done for you! Have a read of this Android article about it.
This is what a database is for. Use it.
Say I have the following my strings.xml file:
<string name="string0">Lorem</string>
<string name="string1">ipsum</string>
<string name="string2">dolor</string>
In my activity an ID is set based on the clicking of a button. If the top button is clicked the id is 0, middle is 1 and bottom button is 2.
What would the syntax look like for referencing one of the three strings?
I know R.string.string0 works but I want to do something equivalent to:
R.string["string"+currentID]
where I derive the string to use based on the ID.
Just not sure what the syntax would look like in Java/Android.
Thanks in advance,
Tony
Could you not just use a string array in your resources instead of separate string entries?
That's a bad approach. It's slow. It'd be better to have an internal integer array with all the R.string IDs.
If you really insist on using a string-based approach, use Resources.getIdentifier().