Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to align an image center relative to the textview

enter image description here

I have an text view as show above and an info icon next to the text view. All is fine here but I wish to have the info icon aligned center to the textview. As of now it is aligned top right relative to the text view. I wish it to be aligned center right relative to the text view

The XML Layout file is below

        <RelativeLayout
             android:orientation="horizontal" 
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
              android:gravity="center"
            android:layout_gravity="center"
            > 

        <TextView               
            android:id="@+id/ViewMessageOne"                
            android:layout_width="275dp"
            android:layout_height="45dp"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#000000"
            android:textSize="14sp"
            android:typeface="sans" 
            android:background="@drawable/back"
            />   

         <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/banner_portrait"
            android:layout_toRightOf="@+id/ViewMessageOne"
            android:paddingLeft="10dp"
             android:gravity="center"               
           />


         </RelativeLayout>

Please point me in the right direction. Thanks for your help

like image 211
Timothy Rajan Avatar asked Nov 15 '13 18:11

Timothy Rajan


2 Answers

Try:

android:layout_gravity="center_vertical|right"

EDIT:

You could also just change the height to match the TextView, as your ImageView gravity is set to center.

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="45dp"
    android:layout_toRightOf="@+id/ViewMessageOne"
    android:gravity="center"
    android:paddingLeft="10dp"
    android:src="@drawable/banner_portrait" />
like image 77
Evan Bashir Avatar answered Oct 14 '22 20:10

Evan Bashir


Unless you have a specific need for the ImageView, you could take that out and use drawableRigh on your TextView

<TextView               
        android:id="@+id/ViewMessageOne"                
        android:layout_width="275dp"
        android:layout_height="45dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textSize="14sp"
        android:typeface="sans" 
        android:background="@drawable/back"
        android:drawableRight="@drawable/banner_portrait"   // here
        />   
like image 38
codeMagic Avatar answered Oct 14 '22 20:10

codeMagic