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

15,368

Solution 1

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

Solution 2

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 );
Share:
15,368
vineet
Author by

vineet

Updated on June 05, 2022

Comments

  • vineet
    vineet almost 2 years

    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