How to access values from "strings.xml" dynamically in android? - 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.

Related

Android-Studio: How to make a resource string value the same as its name

I want to use the string resources to save some key values. So, I have something like this:
<string name="key_name">key_name</string>
The resource name and the resource value are the same. There is no need for me to make a difference between them, as it is just to cleanly store one value.
To reduce redundant information,
is there a way to just tell android-studio that the name equals the value? Something like this?
<string name="key_name"/>
I think you can't do it. It looks like you create a variable, you must name it and when you need you can set its value.
You can't create a variable with same values with name at the same time.
I think you have the wrong resource type. A string resource is used for mapping a name to a text. From what you're describing here you only need the name (and then can extrapolate the text from it).
Using strings with matching name and value shouldn't be necessary:
If you're going to be referencing them from Java or Kotlin code, you still need to know the name (i.e. R.string.key_name), so you might as well just use the string value there.
If it's for using inside XML code (where you want to pass the value of a string, e.g. text="#string/key_name") most of the time you can just use the raw string there (i.e. text="key_name").

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

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

Set values to text identifier by var name. An alternative to getIdentifier

I am trying to set String values for ids defined in xmls. I have defined the set of text which can be assigned to different ids in the layout xml files. Thereby I can also see the int values that are associated with different texts in R.java.
I have stored the variable names that I have given to the texts in my database along with the R.id prefix as they appear in R.java file.
For setting text,
TextView messageone = (TextView)findViewById(R.id.textfield1);
Normal Usage:
String message = "Hi, hello";
messageone.setText(Status);
What i want to implement:
public static final int messagestring is present in R.java
R.id.messagestring is stored in sqlite database in text format
messageone.setText(what_here);
what_here = a way to get the value from "R.id.messagestring" string as obtained from database.
I know public int getIdentifier (String name, String defType, String defPackage)
can be used here. The only change would be stored text in database will change from R.id.messagestring to messagestring. But there is a note discouraging this type of implementation.
It says: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.
Android Docs getIdentifier
I think although this method seems like a longer implementation, can be efficient when the objects dealt with are not text.
There's a getString(int Id) method you can use inside an Activity. It will return a String from a given R.string Id.
String something = getString(R.string.something);
I hope I helped out a bit here, because I'm not entirely sure if I understand your question.

Custom Resource name 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());

Categories

Resources