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..."
SpannableString spannableString = new SpannableString("@");
Drawable d = getResources().getDrawable(R.drawable.your_drawable);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BOTTOM);
spannableString.setSpan(span, spannableString.toString().indexOf("@"), spannableString.toString().indexOf("@")+1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
yourTextView.setText(spannableString);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With