8

I have the following ImageView and TextView: enter image description here

Here is the XML:

<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/headerLinearLay" android:orientation="horizontal">
        <ImageView android:src="@drawable/icon" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/avatarImageView"></ImageView>
        <TextView android:layout_height="wrap_content" android:id="@+id/usernameTextView" android:text="TextView" android:layout_width="wrap_content" android:paddingLeft="4px"></TextView>
    </LinearLayout>

How can I make the image and the text be positioned at the same height? I also want the ImageView to be in the corner

Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556
  • 1
    Possible duplicate of [Android: Using alignBaseline for an image following text](http://stackoverflow.com/questions/5102532/android-using-alignbaseline-for-an-image-following-text) – AdamHurwitz Jul 20 '16 at 20:11

3 Answers3

21

Try this:

<RelativeLayout
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:id="@+id/headerLinearLay">
    <ImageView
        android:id="@+id/avatarImageView"
        android:src="@drawable/icon"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"></ImageView>
    <TextView
        android:id="@+id/usernameTextView"
        android:text="TextView"
        android:paddingLeft="4dp"
        android:layout_toRightOf="@id/avatarImageView"
        android:layout_centerVertical="true"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"></TextView>
</RelativeLayout>
neteinstein
  • 17,529
  • 11
  • 93
  • 123
pawelzieba
  • 16,082
  • 3
  • 46
  • 72
  • That sort of works, but the image is still not in the top corner, and the text overlaps the image a bit. – Sheehan Alam May 24 '11 at 19:26
  • Sorry. I missed the "top corner" sentence. To fix it I've removed `android:layout_centerVertical`. The overlaping took place becouse I put wrong id in `android:layout_toRightOf`. Now everything is fixed. Goog luck. – pawelzieba May 24 '11 at 19:35
  • This not answer the question actually.Question is about Linear Layout. "android:layout_toRightOf" is not in Linear Layout. – Nuwan Harshakumara Piyarathna Jun 26 '20 at 13:38
5

Here is a simple solution that works well. Essential here is to add: android:layout_gravity="center_vertical" to TextView

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/icon" />

    <TextView
        android:id="@+id/usernameTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical" />

    </LinearLayout>
Marcin S.
  • 11,161
  • 6
  • 50
  • 63
0
<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:layout_weight="0.04" />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="204dp"
    android:layout_height="200dp"
    app:srcCompat="@mipmap/ic_launcher" />
paul
  • 83
  • 1
  • 10