Search code examples
javaandroidtextviewimageviewandroid-linearlayout

How can I align my ImageView with my TextView in a LinearLayout?


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


Solution

  • 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>