Getting string array from String values folder? - android

In my Values folder for my project, I have a string array named currency_avatar. I want to be able to use this array of strings in my activity.
I know that "R.array.currency_array" this line of code is legal, however I do not know how to actually get this to refer to a local String array in my own activity.
So, how do I refer this currency_array located in my values folder of res to a local String array variable in my activity?
Sorry for such a simple question. :)

String[] currencies = getResources().getStringArray(R.array.currency_array)

This article will help you in following problems you can face.

Related

Can you select a string resource with a name like

I am thinking of creating a small dynamic layout using strings.xml as a base.
I know how to select single resources but is there a way of selecting ALL resources with a name like "Intro_"
A bit like a Select where like query.
So if i had some strings like;
Intro_Welcome
Intro_FirstParagraph
SecondScreen_Welcome
List of strings mList = select all string resources with a name like "Intro_"
mList would now contain the string of Intro_Welcome and Intro_FirstParagraph
Any help is usefull
Thank you
You can't do this like you described. The string resources name that you define in your xml file are mapped into int values. So, when you use they in your code, you use by the int value created.
You may consider using a modified version of this class, that can get the string resource id within the string text.

How to reset string value from java in Android?

I'm a beginner in Android, so I hope everyone can help me to change String in strings.xml from programming.
Here is the original string:
<string name="cv_name">Hong Sengheng</string>
And I want to reset it's value to Sengheng.
<string name="cv_name">Sengheng</string>
So, hopefully everyone give me idea.
One thing what you have to understand here is that, when you provide a data as a Resource, it can't be modified during run time. For example, the drawables what you have in your drawable folder can't be modified at run time. To be precise, the "res" folder can't be modified programatically.
This applies to Strings.xml also, i.e "Values" folder. If at all you want a String which has to be modified at runtime, create a separate class and have your strings placed in this Class and access during run time.
You can not change the value of String.xml at runtime but you can change the text of that particular Ui where you pass that String...
You cann't change the value of string.xml at the runtime for this u can make a seprate class and change string value at the run time and use it
while the value of a resource should not be changed programmatically you can certainly assign a string resource to a variable String mystring = getResources().getString(R.string.mystring_res); and then change that variable's value mystring="new value";

Get string from xml (other than strings.xml)

I want to get a string array from other file than strings.xml, because I want my project to be organised.
For example, I have this code:
String[] column1 = getResources().getStringArray(R.array.column1);
But my string array isn't located in strings.xml, but in \res\values\tables\10.xml
How can I get the string array from that file?
Thanks in advance.
Your XML files can have arbitrary names, but they need to be located in the values folder. I suggest just calling your file tables10.xml.

How to access a string from a string Array?

I'm brand new to android developing, so I'm sorry if this is a terrible rookie mistake.
That being said, I know in java, if I have a string array named example, I could just do
example[0] to access the first string. I'm basically making a quiz app where it will put answers in a random order, but I don't believe I can just accesss the string array in string resources (my values/strings.xml) with this method? Thanks.
From here
Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);
You can retrieve Strings from Resources this way:
String string = getString(R.string.hello);
More details here.

Storing strings in android XML resource file and referencing them

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));

Categories

Resources