RobertoAllende
RobertoAllende

Reputation: 9528

How to center an ImageView with two TextView?

How is the xml to do something like this ?.

Sample

I.e. I have two TextView, one below the other, both aligned to the left. Also, on the same "line" I've an ImageView aligned to the right. Lastly i need to center vertically the image with the TextViews.

Upvotes: 1

Views: 1599

Answers (3)

j__m
j__m

Reputation: 9625

You could feasibly achieve the desired display with a single TextView widget. You can use a SpannableString or SpannableStringBuilder to display text with mixed formatting and TextView.setCompoundDrawables or TextView.setCompoundDrawablesWithIntrinsicBounds to display an image to the right of the text.

Upvotes: 1

Parijat Bose
Parijat Bose

Reputation: 390

Try this, its very simple and the layout will not get distorted in any resolution which using a relative layout can distort...

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="5" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

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

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </LinearLayout>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_weight="4"
        android:src="@drawable/ic_launcher" />
  </LinearLayout>

</LinearLayout>

Upvotes: 1

Abhi
Abhi

Reputation: 9005

Take RelativeLayout as the parent,Take two in textviews at left of the parent ,below one another. And place ImageView at parent right, vertically center

Check this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="10dp"
    android:paddingBottom="10dp">

    <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" 
     android:id="@+id/textview1"   
     android:layout_alignParentLeft="true"
     android:layout_alignParentTop="true"
     android:layout_marginLeft="5dp"
     android:textColor="#000000"
     android:textSize="17sp"
     />
     <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" 
     android:id="@+id/textview2"   
     android:layout_below="@+id/textview1"
     android:layout_alignParentLeft="true"
     android:layout_marginLeft="5dp"
     android:layout_marginTop="5dp"
     android:textColor="#000000"
     android:textSize="17sp"
     />
    <ImageView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true"
     android:layout_centerVertical="true"
     android:layout_marginRight="5dp"
     />


</RelativeLayout>

Upvotes: 4

Related Questions