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().
Related
I am working on a project that includes a lot of strings and string arrays. I would like to put them into created folders inside res/values, but I get errors when I try to do this. Either getRecources() does not recognize the new folder or the xml attributes cannot link together. I know this is a noob-ish question, but thanks for the help!
Unfortunately, you can't create any subfolders in your values folder. But you have two instruments to control the hierarchy.
String arrays are declared in the following way:
<string-array name="arr_name">
<item>Text</item>
<item>Another text</item>
</string-array>
You can access them through R.array.arr_name.
Prefixes are kind of obvious, but since you mentioned that you are a novice, it's worth mentioning. I usually prefix all of my strings depending on how they are used. For example, btn_ for the text used on buttons, dialog_ for strings used in dialogs and so on. This way autocomplete in the IDE also works much better too.
Also you can split your declarations into different files, but this doesn't have any impact at all on the way you access them, so I don't know if this can help you.
You can define array of strings using following way. Later you can access it in code with R.values.langs
<string-array name="langs">
<item>бг</item>
<item>en</item>
<item>ру</item>
</string-array>
To organise my res folder I use defined xml files not sub-folders.
Basic Example:
- if you have Strings for your Login Page put them in login_strings.xml
- if you have Strings for your Options Page put them in options_strings.xml
etc.
Hope this helps.
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));
I'm new to Android programming. I spent a long time trying to look for a 'phantom line breakpoint' error in my code and I couldn't figure it out till I changed a string resource name.
I have an options.xml menu that contains
<item android:id="#+id/menu_about"
android:title="#string/menu_about"
android:icon="#drawable/ic_menu_about"></item>
My question - I have #+id/menu_about and #string/menu_about. is this possible?
Thank you very much
Yes,its possible,and you can retrieve R.string.menu_about and R.id.menu_about.
and in autogenerated R.java have diffrent class for that.
Yes, this is possible.
R.java itself is organized into some subclasses for strings, ids, drawables etc. It is possible to have two different resources with the same name as they will be members of different classes. However, you cannot have two of the same resources with the same name, like two strings named "foo" for example.
#+id/menu_about creates the item element with id "menu_about".... #string/menu_about in your code sets the title to whatever is stored in strings.xml file in your values folder (i.e. R.string.menu_about)
Yes you can give the same name of #+id& #string.
This both were store in different class in R.java. Like if You declare #+id/menu_about this will store in id class. etc..
I have a problem that I want to show a bulleted list contents which is resided in strings.xml file as an array elements. Then the problem is that how to convert the array elements in Html List format? Can any one suggest any solution regarding the same.
Thanks in advance
I just put the symbol directly into the strings.xml without any codes or anything:
<string name="msg_sms_no_note">• Notes and attachments will not be sent.</string>
There's a problem with the approach suggested by some of the answers in this thread of prepending the bullet unicode character (i.e. \u2022) to each of the Strings in the String array: You don't get proper indentation when one or more Strings in the String array span multiple lines. What you get is formatting as follows:
In order to get proper indentation, you're better using BulletSpan. In doing so, you'll get formatting as follows:
To use BulletSpan, you need to create a SpannableStringBuilder instance and append each String in your String array to this SpannableStringBuilder instance. As you append each String, call the setSpan(what:start:end:flags:) method on the SpannableStringBuilder instance passing in a BulletSpan instance for the what parameter. You can find an example of this in the appendBulletSpan(...) Kotlin extension function located here.
I think, the most elegant way of doing this is to load a WebView and put your string in it. this way, you use the common ul/li convention and you can style it at your leisure with CSS.
Use the unicode escape sequence "\u2022" in strings.xml
like so:
<string name="menu_new_trip_desc">View them in: \n\u2022 Table
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.