nrofis
nrofis

Reputation: 9766

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

Upvotes: 7

Views: 9429

Answers (3)

Akash Bisariya
Akash Bisariya

Reputation: 4754

    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);

Upvotes: 12

Kevin Coppock
Kevin Coppock

Reputation: 134664

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);

Upvotes: 6

koral
koral

Reputation: 2533

You can use custom view or custom title with ImageView or TextView containing compound drawables.

Upvotes: 0

Related Questions