I want to get a string resource name or ID by passing its value.
Example :
<string name="stringName">stringValue</string>
I want to pass the stringValue and get the stringName or ID (id value)
Based on ID i will do some calculation to get another ID of another String resource
I don't know if that's possible and I think the IDs can change without warning if the R class gets newly generated. You could of course try some reflection magic on this class, but I would recommend against it.
I also have this problem, and can think of 2 possible workarounds:
load all the strings and their names into a table, and look in the
table.
Or cycle through my complete list of names, getting the string resource
for each one, and comparing it to my known string resource.
I am implementing the 2nd one, as my list of string resources is not very big, and I don't have to do this operation very often. Once the name is known, it's possible to get the Resource Id via:
//Get resource id from name
var resourceId = (int) typeof (MyApp_droid.Resource.String).GetField(MyStringName).GetValue(null);
(code is C# because I'm working in Xamarin).
Related
So I have string.xml files for two languages, english and swahili. I let my users choose their default languages. And everything goes well until when I save the string resource ID for later use when they decide to change the language from the application's settings,but whenever I edit or add another string resource,and update my application, the original string ID's change and the string resource ID's that I saved in the database end up fetching values from different string resource from what my users had selected earlier. How do I overcome this? or stop my string resource ID's from changing every time I add new strings.
And everything goes well until when I save the string resource ID for later use
That is not a good plan.
but whenever I edit or add another string resource,and update my application, the original string ID's change and the string resource ID's that I saved in the database end up fetching values from different string resource from what my users had selected earlier
That is why this is not a good plan. Resource IDs are not designed to be constant from build to build.
How do I overcome this?
Do not store string resource IDs in a file, database, Web service, or similar container. Instead, store some identifier that you control that allows you to determine what string resource ID(s) that you should use.
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").
I'm trying to understand an Android app that performs computations on investment portfolios. The portfolios are stored in res/values/portfolio.xml:
When a button is pressed in the app, the portfolio data is retrieved as follows:
String portfolioName = ((TextView) findViewById(R.id.portfolioName)).getText().toString();
Resources res = getResources();
String[] data = res.getStringArray(res.getIdentifier(portfolioName, "array", this.getPackageName()));
I found the Android documentation on the String Array resource type that explains the syntax of the portfolio.xml file, and it explains why the name attribute should be used as the first argument of getIdentifier():
“The filename is arbitrary. The <string-array> element's name will be used as the resource ID.”
But I haven't found any documentation that explains how you know what you're supposed to put for the defTypeargument of getIdentifier (other than that it's a string). In the provided example, "array" works, but where does it come from? And what are the possible values of 'defType' in general?
getIdentifier returns the id of the resource for the given resource name. typeDef refers to the type of the Resource (read more here). Keep in mind that the content of res is parsed at compile time and the R.java class is generated from the result of this parsing. In the end what you are looking for is a field declared in that class. I don't know the internal implementation, but if you provide array as res type, android will look up only on R.array, instead than on the whole R
I am trying to get the ID of a string with value "Hello" from strings.xml doing to following:
getContext().getResources().getIdentifier("Hello", "string", getContext().getPackageName());
It returns 0. Why is this returning 0?
Because you're passing the string value, while you should pass the resource name of the string.
Look at the method signature: getIdentifier(String name, String defType, String defPackage).
When the resource is not found, 0 is returned which is consistent with what you're experiencing.
Edit:
Use of getIdentifier() is discouraged - to quote the reference:
Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.
I know no direct way of getting the ID of a string based on value of the string in question. However I've seen two workarounds/hacks:
Name all strings for which you want the lookup to be performed like this: stringN where N is for example 0-based integer incremented for each of the strings. You end up with ugly names like string0, string1, etc. Then you do the lookup yourself - you iterate over N, create string ID: stringN, get its value and check for a match. This is ugly in my opinion.
Use Typed Array resource and put all your strings there. It's basically the same as method 1, but avoids creation of nasty, polluting resource names.
In case package name in Manifest differs you won't get an identifier because your package name is incorrect, in order to get correct package name for resources dynamically you should use:
String rPackageName = mContext.getResources().getResourcePackageName(R.some.wellKnownResource);
'rPackageName' is a package name that you should pass to the function
getIdentifier(String name, String defType, String defPackage)
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.