0

I am trying to have a TextView right next to an ImageButton. However, the TextView isn't aligned well. I want it to start at the same top position as the ImageButton.

This is how it looks right now:

enter image description here

Here is my layout:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="10dp"
    android:orientation="horizontal">

    <ImageButton
        android:id="@+id/myimage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:src="@drawable/myimage"/>

    <TextView
        android:id="@+id/mytexview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14dp"
        android:text="My Text"
        android:textStyle="bold"
        android:layout_marginLeft="5dp"
        android:textColor="#DF533B"
        />

</LinearLayout>
birdy
  • 9,286
  • 24
  • 107
  • 171
  • Try to use ImageView instead of ImageButton becz ImageButton set image over button so image is not cover whole area so try to use ImageView which cover whole space which is allocated to it. – Haresh Chhelana May 21 '14 at 12:26

5 Answers5

1

Try this..

Add android:layout_gravity="center" for both ImageButton and TextView

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="15dp"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/myimage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#FFFFFF"
        android:src="@drawable/myimage" />

    <TextView
        android:id="@+id/mytexview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="5dp"
        android:text="My Text"
        android:textColor="#DF533B"
        android:textSize="14dp"
        android:textStyle="bold" />
</LinearLayout>

Or Use android:drawableLeft="@drawable/ic_launcher" and add android:gravity="center" for that TextView

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="15dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/mytexview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="5dp"
        android:drawableLeft="@drawable/ic_launcher"
        android:gravity="center"
        android:text="My Text"
        android:textColor="#DF533B"
        android:textSize="14dp"
        android:textStyle="bold" />
</LinearLayout>
Hariharan
  • 24,741
  • 6
  • 50
  • 54
1

set this property to view element android:gravity="top" inside the TextView tag.

I hope this will work.

Lavekush Agrawal
  • 6,040
  • 7
  • 52
  • 85
1

Try that:

<TextView
        android:id="@+id/mytexview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14dp"
        android:drawableLeft="@drawable/myimage"
        android:text="My Text"
        android:textStyle="bold"
        android:gravity="center_vertical"
        android:layout_marginLeft="5dp"
        android:textColor="#DF533B"/>
Engr Waseem Arain
  • 1,163
  • 1
  • 17
  • 36
0

You have to do android:layout_height="match_parent" and set gravity center .

use below code:-

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="15dp"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/myimage"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        android:src="@drawable/myimage" />

    <TextView
        android:id="@+id/mytexview"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="5dp"
        android:text="My Text"
        android:gravity="center"
        android:textColor="#DF533B"
        android:textSize="14dp"
        android:textStyle="bold" />
</LinearLayout>
duggu
  • 37,851
  • 12
  • 116
  • 113
0

try,

android:gravity="top" in textview

Chris Barlow
  • 3,274
  • 4
  • 31
  • 52
Akash Moradiya
  • 3,318
  • 1
  • 14
  • 19