nrofis nrofis - 1 year ago 114
Android Question

Drawable in string resource

I want to show

that shows its message with string and icons together.

Is it possible to insert icons/images/drawables in string resource? Is there any way to show drawables with the string in the


If its was not clear, the drawables need to be inside the string. like "click the icon [icon-image] and then click on..."

Answer Source

The AlertDialog.Builder class has a method setIcon(int iconRes) or setIcon(Drawable icon) that you can use for this.


If you need it in the middle of the string, you could use an ImageSpan:

String src = "Here's an icon: @ isn't it nice?";
SpannableString str = new SpannableString(src);
int index = str.indexOf("@");
str.setSpan(new ImageSpan(getResources().getDrawable(R.drawable.my_icon), index, index + 1, ImageSpan.ALIGN_BASELINE));

AlertDialog.Builder x = new AlertDialog.Builder(myContext);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download