nrofis nrofis - 3 months ago 16
Android Question

Drawable in string resource

I want to show

AlertDialog
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
AlertDialog
.

EDIT

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

Answer

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

EDIT:

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);
x.setMessage(str);
Comments