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().
Related
I have a string I'm using in a text view for example
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvText"
android:text="#string/yes"/>
I want to be able to get the key "yes" from this Textview , not the value of this string but just the key that leads to it , is there some way I can use the value of the string to get it ?
there is no option for getting string resource id (you are naming it "key") after setting with android:text. TextView is resolving it in its runtime, fetching String and "forgets" way how it was set up. same case when setting e.g. image for ImageView (android:src="#drawable/drawable_name") or some dimensions for all Views (e.g. android:padding="#dimen/some_dimension")
if you really need this reference in code then it would be better to NOT set this text via XML, instead store this reference under some final int variable in code and use it for setting text for TextView programmatically, further use same variable in another place you need
I want to print ŒHI 5¹ in my TextView. So I have simply written :
tv.setText("Welcome to " + R.string.app_name)
where R.string.app_name is <string name="app_name"><b>ŒHI 5¹</b></string>
But the strange thing is textview is showing a number
The number is: 2131230755
I have no idea why this is happening.Please help.
use getString(R.string.app_name)
or
getResources().getString(R.string.app_name);
R.string.app_name is just a long number genereted to identify that resource. The result of string + long is just that long concatenated to that string. so you need to get the string corresponding to that identifier.
Actually every resource (layout, drawable, array, string) gets an identifier, these are put in the R file. layout identifiers are kept together in an inner class called layout, strings in string and so on.
use :
getString(R.string.app_name);
or if you are not in an activity then
mContext.getResource().getString(R.string.app_name);
You can't call direct R.string for your requirement .
Pass getResource().getString
Returns the string value associated with a particular resource ID
Finally
getResource().getString(R.string.your_string);
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 have an android application that use a gallery component to choose an icon and assign it to a button.
The icon set is located in res/drawable folder and is accessed in the gallery with the typical adapter of the guide:
private Integer[] Imgid = {
R.drawable.icon_home,
R.drawable.icon_home2,
...
}
After an icon choosing, i stored the settings in a db with id of the button and of the drawable.
All works done, but i've noticed that if i'll want to add or modify my icon set or resources in general of my application, ids of old resource could change, so the preferences in the db refers to wrong icon.
Is there a way to force the old id of R class so they didn't change in the future? Or is there another way to manage the item of the component galley with another attribute than id? (maybe string name and reflection?)
Thanks in advance
You can store the name of the drawable in the database if you don't plan to change that. getResourceEntryName returns the name from the resource id:
Resources resources = getResources();
String name = resources.getResourceEntryName(R.drawable.icon);
And you can get back the resource id from the name with getIdentifier:
int resId = resources.getIdentifier(name, "drawable", "your.package.name");
You can't use static int for resource identifier, however you should look at two methods od Resources class:
getIdentifier()
getresourceName()
You shouldn’t rely on the actual values of the R.drawable.* attributes.
Create your own ids instead (for example 1 correspond to R.drawable.icon_home and 2 correspond to R.drawable.icon_home2)
Edit:
String name and reflection should work too, but it’s probably a little overkill you have only a few icons.
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