Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add imageview inside textview or add textview inside imageview, android?

I was wondering if it is possible to add textview inside imageview or vice-versa. Is it possible? If yes then how?

I want to make this at runtime

like image 270
vineet Avatar asked Dec 10 '25 00:12

vineet


2 Answers

The simplest way I think is to setCompoundDrawables via XML attributes

android:drawableLeft
android:drawableTop
android:drawableRight
android:drawableBottom.

Example:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableRight="@drawable/ic_navigation_chevron_right"
    android:text="Next "
    android:textSize="20dp" />

Result:

textView with image inside

like image 172
Andrew Avatar answered Dec 11 '25 14:12

Andrew


ImageSpan ispan = new ImageSpan(context, yourresourceid);
text.setSpan(ispan, index, index + strLength, 0);

OR

    TextView textView = (TextView)findViewById( R.id.TextView );
    Spannable spannable = (Spannable)textView.getText();
    StyleSpan boldSpan = new StyleSpan( Typeface.BOLD );
    spannable.setSpan( boldSpan, 41, 52, Spannable.SPAN_INCLUSIVE_INCLUSIVE );

    TextView textView2 = (TextView)findViewById( R.id.TextView2 );
    SpannableStringBuilder ssb = new SpannableStringBuilder( "Here's a smiley  " );
    Bitmap smiley = BitmapFactory.decodeResource( getResources(), R.drawable.emoticon );
    ssb.setSpan( new ImageSpan( smiley ), 16, 17, Spannable.SPAN_INCLUSIVE_INCLUSIVE ); 
    textView2.setText( ssb, BufferType.SPANNABLE );
like image 31
Sagar Maiyad Avatar answered Dec 11 '25 14:12

Sagar Maiyad