I'm new into the "Android World" and I've a problem getting a ImageView value, suppose I've a ImageView somewhere in my layout file and I want to retrieve is value, i.e. the value in the "android:src" tag. How I can I do it?
Thanks in advance.
Once the resource file is compiled the ImageView simply refers to a resource integer in the R file. So the image "src" value will simply be a value once you've retrieved it, and not the string value you placed in your layout file.
Nonetheless, I think you can get that ID through your ImageView's getResources() method.
EDIT: I'm wrong, turns out you can get the string through Resources.getString(int id)
EDIT 2: Looks like getResources might not contain the ID for you either, an Android engineer already answered a similar question: http://groups.google.com/group/android-developers/browse_thread/thread/84d31b244163821d?pli=1
Related
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 have a lot of images (more than 200) with various sizes in mipmap folders, and I need to set images in ImageView dynamically... but how to do this without "direct description" in my code of that R.mipmap.*int files?
There are flag images in mipmap folders, and my object has it's own String field "flagCountry" which consists of two characters. Names of flags in mipmap folders consist of same characters. So, that's a trouble - how to dynamically set setImageResource(R.mipmap.XXX) from flagCountry String variable?
I'm thinking that decision in "reflection", but how is it possible? )) Please help
You can use Resources.getIdentifier() to find the integer id for a resource given its name. It's like reflection for android resources.
int id = getResources().getIdentifier("XXX", "drawable", getPackageName());
Or something along those lines. This assumes the code is in an activity. Then you can call setImageResource(id) with that id.
As said by Doug, you can use Resources.getIdentifier() to find the integer id for a resource given its name. But you said you wanted the resource from mipmap. So do a simple change. Change the word "drawable" to "mipmap", and you'll get the image from mipmap. The changed code:
int id = getBaseContext()getResources().getIdentifier("XXX", "mipmap", getBaseContext().getPackageName());
image.setImageResource(id);
I need to set the notification image icon dynamically , whose name is passed to my class dynamically.
For that , I need to set the builder.setSmallIcon(int res_id). I googled it , but people have used the example of setDrawableImage()
I have seen this post , and accordingly implemented in my code.
//blah blah lines of code
String uri = "#drawable/a";
int imageResource = con.getResources().getIdentifier(uri,null, con.getPackageName());
Drawable res = con.getResources().getDrawable(imageResource);
builder.setSmallIcon(res);
//blah blah code lines
Of course, this shows error because setSmallIcon() requires integer to be passed to it. con is the context passed to my constructor.
I have seen How to set the image from drawable dynamically in android? post
Of course, this shows error because setSmallIcon() requires integer to be passed to it.
You already have the desired integer. You named it imageResource. Pass imageResource to setSmallIcon().
I search for a way to say which resource type is behind a integer ID. For example i have the id of a drawable and another one for a color. I can load the drawable with Resource.getDrawable(id) and the color with Resource.getColor(ID) but how can i know which method is the right one for a given ID. I look for something like Resource.isColor(id)?
Has anybody a good idea. I wrote a image loader which loads images from web, filesystem and drawables but now also from hex color or color ID.
Check out this function from android Resources class. Resources.getResourceTypeName(int resid) (also this )
To find the resource type the safest solution is to use the below code
String viewName = findViewById(resourceId).toString();
String resourcetype = viewName.substring(0,viewName.indexOf("{"));
The resoucetype then contains the full classname for the resource, eg in case of EditText, it shows
android.widget.EditText
Tried some other ideas but noted that getResourceTypeName only returns 'id' as value and getTag() on View does not work in all conditions.
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));