How to insert an ImageView at the end of a multi-line TextView?

This may be a dumb question, but I can't find a way to insert a small image at the end of the second line of the TextView. The image is always displayed to the right of the entire text, not the end of the line.

I want to insert an image as follows:

TextTextTextTextTextTextTextTextTextText TextTextTextText. <ImageView> 

I get:

 TextTextTextTextTextTextTextTextTextText <ImageView> TextTextTextText. 

I hope there is a way to do this.

Src:

 <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TESTESTESTESTESTESTESTESTESTESTESTESTES" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#FFFFFF" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/tv" android:src="@drawable/icon" /> </RelativeLayout> 
+7
source share
5 answers

create an ImageSpan and add it to the text box. So you can put your image wherever you want in the text

Example -

 ImageSpan imagespan = new ImageSpan(appContext, ressource); text.setSpan(imagespan, i, i+ strLength, 0); //text is an object of TextView 
+12
source

The best way to complete this is as follows ...

 ImageGetter getter = new ImageGetter(){ public Drawable getDrawable(String source){ // source is the resource name Drawable d = null; Integer id = new Integer(0); id = getApplicationContext().getResources().getIdentifier(source, "drawable", "com.sampleproject"); d = getApplicationContext().getResources().getDrawable(id); if (d != null) d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); return d; } }; String imgString = this.getResources().getString(R.string.text_string) + " <img src=\"img_drawable_image\"/>"; ((TextView)findViewById(R.id.textview_id)).setText( Html.fromHtml(imgString, getter, null)); 
+4
source

This may not be the best solution, but you can try Html.fromHtml to insert the image into your line.

 ImageGetter getter = new ImageGetter(){ public Drawable getDrawable(String source){ Drawable d = null; context.getResources().getDrawable(Integer.parseInt(source)); if (d != null) d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); return d; } }; String imgString = <source string> + " <img src=\"R.drawable.icon\"/>"; ((TextView)findViewById(R.id.tv)).setText( Html.fromHtml(imgString, getter, null)); 
0
source
 SpannableString ss = new SpannableString(title); Drawable d = getResources().getDrawable(R.drawable.gallery_list); d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE); ss.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); newsTextView.setText(ss); 
0
source
 TextView textView =new TextView(this); SpannableStringBuilder ssb = new SpannableStringBuilder( "Here a smiley how are you " ); Bitmap smiley = BitmapFactory.decodeResource( getResources(), R.drawable.movie_add ); ssb.setSpan( new ImageSpan( smiley ), ssb.length()-1, ssb.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE ); textView.setText( ssb, BufferType.SPANNABLE ); 
0
source

All Articles