Need Integer resource id of drawable resources dynamically

269 views Asked by At

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

1

There are 1 answers

0
CommonsWare On BEST ANSWER

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().