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.
Asked
Active
Viewed 2,371 times
0

Parag Chauhan
- 35,760
- 13
- 86
- 95

PaolaJ.
- 10,872
- 22
- 73
- 111
-
1http://stackoverflow.com/a/5562355/2001247 Check this out. This is really easy. – ElDuderino Mar 23 '14 at 11:52
1 Answers
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
-
Read it too quickly didn't see how you were assigning. This is correct – zgc7009 Mar 23 '14 at 11:52
-
This is not a good idea. It's easier with this one http://stackoverflow.com/a/5562355/2001247 – ElDuderino Mar 23 '14 at 11:53