Atul Bhardwaj
Atul Bhardwaj

Reputation: 6707

Put imageView just after textview in android

I am getting a problem in putting imageview just after textview if textview becomes of two line. I want to show this(Image just after text,text can be in one line,two line or three lines) enter image description here

My imageview comes just after textview if text is short(one line). But when text becomes longer(two lines) then image goes to next line like this enter image description here

I know the reason of this problem but don't know how to solve?

Upvotes: 4

Views: 4562

Answers (4)

Atul Bhardwaj
Atul Bhardwaj

Reputation: 6707

After lot of googling, I found a good solution of my problem

TextView textView =new TextView(this);
                SpannableStringBuilder ssb = new SpannableStringBuilder( "Here's 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 );

With the help of above code, you can add image any where in Textview.

Upvotes: 7

Roman Kazanovskyi
Roman Kazanovskyi

Reputation: 3599

instead of ImageView try clickable="true" and drawableEnd

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"        
    android:drawableEnd="@drawable/navigation_icon"
    android:text="Text"
    android:clickable="true"
    />

Upvotes: 2

preeya
preeya

Reputation: 169

Use the following layout

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
     android:padding="2dp"
    android:background="@android:color/transparent"
    >
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     android:gravity="left"
     android:padding="1dp"
    android:layout_gravity="left"
     android:orientation="horizontal" >

    <TextView
        android:id="@+id/contentText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Medium T"
          android:gravity="center"
            android:minLines="2"
         android:layout_gravity="center"
         android:textColor="@color/white"
          android:padding="5dp"
          android:visibility="invisible"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    <ImageView
        android:id="@+id/contentImage1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:padding="5dp"
        android:visibility="invisible"
        android:src="@drawable/browse_product_thumbnail" />


</LinearLayout>

</LinearLayout>

-Preeya

Upvotes: 0

Bhavesh Patadiya
Bhavesh Patadiya

Reputation: 25830

try to make your xml look like below.

   <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:text="TextView" />

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/my_icon" />
        </LinearLayout>

    </LinearLayout>

Upvotes: -1

Related Questions