Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 638
nrofis Avatar asked Sep 29 '13 20:09

nrofis


2 Answers

    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);
like image 187
Akash Bisariya Avatar answered Oct 04 '22 12:10

Akash Bisariya


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);
like image 32
Kevin Coppock Avatar answered Oct 04 '22 10:10

Kevin Coppock