0

How to combine text image text inside TextView ? I load text from values/strings.xml but how to add at the middle od text one image icon.png from drawable.

Parag Chauhan
  • 35,760
  • 13
  • 86
  • 95
PaolaJ.
  • 10,872
  • 22
  • 73
  • 111

1 Answers1

1

Why don't you put something like this in your layout:

<RelativeLayout android:layout_width="wrap_content"
                android:layout_height="wrap_content">

    <ImageView 
            android:id="@+id/ivMiddle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/image"/>

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/text_left"
            android:layout_centerHorizontal="true"
            android:layout_toLeftOf="@id/ivMiddle"/>
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/text_right"
            android:layout_centerHorizontal="true"
            android:layout_toRightOf="@id/ivMiddle"/>

</RelativeLayout>

Has the same effect and is a lot easier to do.

EDIT:

Or as ElDuderino suggested you can simply do this:

ImageSpan is = new ImageSpan(context, R.drawable.image);
SpannableString text = new SpannableString("Left  Right");
text.setSpan(is, 4, 4, 0);
Xaver Kapeller
  • 49,491
  • 11
  • 98
  • 86