I have a short question:
How is it possible to convert a String, containing the Id of a Drawable, which is
String idString = "R.drawable.bubblegum";
to an Integer,
idInt
so that I can use that ID for the reference of an image (, which is used in a SimpleAdapter)
So, to make an example, I can't do that:
bubble.setImageDrawable(this.getResources().getDrawable(idString));
//not possible, cause idString is a String an not an Id/Int
So, I have the String that's containing the id, but unfortunately as a String.
Thanks!
Although this question is rather old already, the thing you're missing is that "id" and "drawable" are different resource types. So instead of
getResources().getIdentifier(stringId, "id", "my.Package");
it's
getResources().getIdentifier(stringId, "drawable", "my.Package");
You can also get package name with the activity context like activityContext.getPackageName()
/**
* Returns Identifier of String into it's ID as defined in R.java file.
* #param pContext
* #param pString defnied in Strings.xml resource name e.g: action_item_help
* #return
*/
public static int getStringIdentifier(Context pContext, String pString){
return pContext.getResources().getIdentifier(pString, "string", pContext.getPackageName());
}
Call getIdentifier() on the Resources object you get via getResources(), as seen in these StackOverflow questions:
Is possible in Android to findView by String id?
Android findViewbyId with a variant string
How to access R.string.xxx resources from a method by passing string 'xxx' as parameter to that method?
Dynamically build a resource Identifier
How to access the values from strings.xml dynamically?
Drawable resource using a variable
among others.
You could try the following
int id = getResources().getIdentifier("arr_name"+positionSelected,
"array", rootview.getContext().getPackageName());
I use in spinner dropdown, get array string follow parent spinner
may help you!
int idInt = R.drawable.bubblegum;
Unless there's something I'm missing here.
if your idString is constant, i.e. it's doesn't change during runtime, follow DeeV's answer.
If it changes, you can take a look at getIdentifier method.
At least, I couldn't get a solution for this problem. It seems that there's no way to convert a String "R.id.mytext" into an integer like R.id.mytext that can be used in findViewById(R.id.myText).
Related
I have a short question:
How is it possible to convert a String, containing the Id of a Drawable, which is
String idString = "R.drawable.bubblegum";
to an Integer,
idInt
so that I can use that ID for the reference of an image (, which is used in a SimpleAdapter)
So, to make an example, I can't do that:
bubble.setImageDrawable(this.getResources().getDrawable(idString));
//not possible, cause idString is a String an not an Id/Int
So, I have the String that's containing the id, but unfortunately as a String.
Thanks!
Although this question is rather old already, the thing you're missing is that "id" and "drawable" are different resource types. So instead of
getResources().getIdentifier(stringId, "id", "my.Package");
it's
getResources().getIdentifier(stringId, "drawable", "my.Package");
You can also get package name with the activity context like activityContext.getPackageName()
/**
* Returns Identifier of String into it's ID as defined in R.java file.
* #param pContext
* #param pString defnied in Strings.xml resource name e.g: action_item_help
* #return
*/
public static int getStringIdentifier(Context pContext, String pString){
return pContext.getResources().getIdentifier(pString, "string", pContext.getPackageName());
}
Call getIdentifier() on the Resources object you get via getResources(), as seen in these StackOverflow questions:
Is possible in Android to findView by String id?
Android findViewbyId with a variant string
How to access R.string.xxx resources from a method by passing string 'xxx' as parameter to that method?
Dynamically build a resource Identifier
How to access the values from strings.xml dynamically?
Drawable resource using a variable
among others.
You could try the following
int id = getResources().getIdentifier("arr_name"+positionSelected,
"array", rootview.getContext().getPackageName());
I use in spinner dropdown, get array string follow parent spinner
may help you!
int idInt = R.drawable.bubblegum;
Unless there's something I'm missing here.
if your idString is constant, i.e. it's doesn't change during runtime, follow DeeV's answer.
If it changes, you can take a look at getIdentifier method.
At least, I couldn't get a solution for this problem. It seems that there's no way to convert a String "R.id.mytext" into an integer like R.id.mytext that can be used in findViewById(R.id.myText).
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 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).
In Android, to access a string from the strings.xml file, we use R.string.string_id. Would it be possible to have a method such that we'll use the string form of string_id? I mean can we for example have a method GetString("string_id") to retrieve R.string.string_id?
//Replace this with appropriate context
String name = "name_of_your_string_in_strings_xml_file_goes_here";
int resId = this.getResources().getIdentifier(name, "string", this.getPackageName());
String string = this.getResources().getString(resId);
It is possible. You have to understand the fact that any data that is pinned to xml files in android are considered as resource to the app. And android resource can be accessed only by means of unique id we provide to them. These ids that are generated are of type int and hence we need to pass a int parameter to get a reference to these resource at run time.
But it is not that only the default way to access them is by using their res value but instead if you could know the name of the string key you could find the id from there and then use the int id to do this.
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.