Custom Resource name Android - android

I could not find an answer to this question anywhere so I have posted it.
Let's say I have multiple resource strings, like:
<string name="hello">Hello World, MainScreen!</string>
<string name="app_name">My Title</string>
And I reference them in my code like so:
R.values.hello
R.values.app_name
But how can I access them like this?
String test = "hello";
String value = R.values.test;
I am trying to do something like this, but on a much larger scale. Or is there a different, but better way of doing it?

In an Activity, this will work...
String value = getString(R.values.hello);
R.values.hello is an integer used as a 'lookup' for the actual string itself. All resources are handled this way.

The correct way to obtain those values would be
String name =
CurrentClassName.this.getString(R.string.hello);
By using CurrentClassName.this you will assure that you can use it inside functions and nested classes.

I think this link might help you http://steven.bitsetters.com/2007/11/27/accessing-android-resources-by-name-at-runtime/
int id = getResources().getIdentifier("hello", "values", getPackageName());

Related

How do you use an integer from the resource file within a string in the same resource file?

Is it possible to use an integer:
<integer name="minstringlength">7</integer>
within the same resource file but within a string:
<string name="nametooshort">Please enter a name longer than #integer/minstringlength characters</string>
I presume you want to use different minimum limits based on device configuration. You can't use the #integer/minstringlength in the string content, so I think the best you can do is have a format argument and build the string yourself like this:
<integer name="minstringlength">7</integer>
<string name="nametooshort">Please enter a name longer than %1$d characters</string>
int nameMinLength = getResources.getInt(R.integer.minstringlength, 0);
String nametooshort = getString(R.string.nametooshort, nameMinLength);
This assumes you are using an Activity/Fragment, otherwise you need a Context in order to use getResources() and getString().
It can not be done in resource file.
As xml contains static content and also from android string-resource doc there is no sub tag or nested tag of <string>
It task can done easily in java file
getResources().getString(R.string.minstringlength);
getResources().getInteger(R.integer.nametooshort);
OR
Follow this post
<string name="meatShootingMessage">You shot %1$d pounds of meat!</string>
int numPoundsMeat = 123;
String strMeatFormat = getResources().getString(R.string.meatShootingMessage);
String strMeatMsg = String.format(strMeatFormat, numPoundsMeat);

How to access values from "strings.xml" dynamically in android?

App version 2.3.3
Here is what i am looking for...
I have few strings in "values/strings.xml". I wish to retrieve the values depending on the string's "name" attribute and use it my application. The input to which string in the xml should be used comes dynamically. Here is an example...
My input from other file :: "A" => This value changes. It can be any value in A,B,C,D.
I have different strings in strings.xml, like...
<string name="A">AforApple</string>
<string name="B">BforBall</string>
<string name="C">CforCat</string>
<string name="D">DforDog</string>
Now, how do i programmatically get the value(AforApple) of the String with name="A".
String str = context.getResources().getString(R.string.A)
or u can use
textBox.setText(R.string.A);
do not forget to import the package com.yourpackackage.R.
You need to use getResources() method:
String a = getResources().getString(R.string.A);
Just for the record, you can also dynamically generate it using reflection.
int stringRes=R.string.class.getField("Here you put the dynamically generated input, such as A").getInt(null);
textView.setText(stringRes);
This will return the resource int value from string XML based on the input, as long as the input value "A" matches string name in the XML, this will retrieve them dynamically.
To access a String resource/value that you’ve defined in an XML file from your Android/Java code, use the Android getString method, like this:
String A = getString(R.string.a_string);
If your code gets a string like "A" and you are trying to dynamically find the string in your resources that matches that name, I don't think you can do that.
Instead of using the strings.xml, you might want to use arrays.xml and build a HashMap from that before you need to access those strings
Try this:
String word = getResources().getString(R.string.A);
Check out the link here.
You can use this code:
getText(R.string.A);
Basically, you need to pass the resource id as a parameter to the getText() method.

Reference string in xml

I'm trying to reference a formatted String in my strings.xml.
I was googling around and found this good article:
http://developer.android.com/guide/topics/resources/string-resource.html
I followed it to every detail it would seem, but the text printed out isn't what it's supposed to be.
First, in my strings.xml under resources, i defined it as:
<string name="print_binary">This is the printout: %1$s</string>
And in the .java i put:
String fromString = "one";
Resources res = getResources();
onScreen = String.format(res.getString(R.string.print_binary), fromString);
This gives the following text on screen:
This is the printout: %1$s
Please advice
You can use:
onScreen = res.getString(R.string.print_binary, fromString);
Goodness, I'd totally missed inputting something like
TextView show = (TextView) findViewById(R.id.tvDisplay);
show.setText(onScreen);
And then adding an id for my TextView object.
Too late in the night. It seems like the String.format() statement isn't necessary, i'll read up on that.
Thanks for your support!

how can i reference something in the /values/strings.xml file programatically?

i had some items in my strings.xml file that i want to change programatically, and originally i was doing it through a setText();call but now i am attempting to translate my app to a different language which means everything needs to be set in my strings.xml file. is it possible to put all the text for my app into a strings.xml and change things programatically through references to the string names, instead of using the setText() function call? for example how would i reference "GrandTotal"?
<string name="GrandTotal">Grand Total:</string>
<string name="choose_prompt">Choose a Mode</string>
You can use setText(R.string.GrandTotal);
If you don't have the possibility to set the text via resId directly you can use getString(R.string.GrandTotal);
To avoid confusion between resourceIds and real ints, you could also use statements like
String s = getResources().getString( R.string.grand_total );
but for most ui methods an overload often provides support for passing directly resourceIds as #Keyboardsurfer said
Try this way . I hope it helps you .
setText(getResources().getString(R.string.GrandTotal));
in an Acrivity:
String str = getString(R.string.choose_prompt);
or
String str = this.getString(R.string.choose_prompt);

Dynamically referencing a String in Strings.xml in Android

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().

Categories

Resources