0

I want to have an image to the left of a text. I want both the image and the text to be at the horizontal center of the screen. So I created the layout below. But while everything looks correct on the eclipse Graphical Layout, on a real device, the image keeps appearing on top of the image, as if I had used drawableTop.

<RelativeLayout
    android:id="@+id/animal"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_below="@id/the_bar"
    android:layout_centerHorizontal="true"
    android:background="#FFFFFF"
    android:clickable="true"
    android:onClick="onAnimalClicked" >

    <TextView
        android:id="@+id/animal_textview"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_centerHorizontal="true"
        android:drawableLeft="@drawable/animal_button"
        android:drawablePadding="5dp"
        android:gravity="center_vertical"
        android:text="Add to Animals"
        android:textColor="#258BE2"
        android:textStyle="bold" />
</RelativeLayout> 
Cote Mounyo
  • 13,817
  • 23
  • 66
  • 87
  • Please post the complete layout file. The problem is not in the code you have posted. If the file is too long to post, use pastebin and provide a link here. – Vikram Aug 27 '13 at 01:49

3 Answers3

1

Try this:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <RelativeLayout
        android:id="@+id/animal"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@id/the_bar"
        android:layout_centerHorizontal="true"
        android:background="#FFFFFF"
        android:clickable="true"
        android:onClick="onAnimalClicked" >

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

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/imageView"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"/>
    </RelativeLayout>
</LinearLayout>
Caleb Bramwell
  • 1,332
  • 2
  • 12
  • 24
1

If you want to overlap a text over an imageview/another view, you can't use RelativeLayout. This is because it is in the nature of the layout to make views repel other views. If a view is on top of another view, it is automatically just pushed to default position and ignores the written attributes.

Thus, solution is to use FrameLayout

                 <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/iv"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical|center_horizontal"
                    android:src="@drawable/bike" >
                </ImageView>


                    <TextView
                        android:id="@+id/tv1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical|center_horizontal"
                        android:gravity="center" >
                    </TextView>
                </FrameLayout>
Danny Kim
  • 310
  • 1
  • 3
  • 10
  • `If you want to overlap a text over an imageview/another view, you can't use RelativeLayout. This is because it is in the nature of the layout to make views repel other views.` Please read the accepted answer here: [Overlapping Views in Android](http://stackoverflow.com/questions/961944/overlapping-views-in-android). One of `RelativeLayout's` strong points is that it _allows_ overlapping of Views. And `FrameLayout` does too. – Vikram Aug 27 '13 at 01:47
0

Try this:

    <TextView
        android:id="@+id/animal_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:drawableLeft="@drawable/animal_button"
        android:drawablePadding="5dp"
        android:gravity="center_vertical"
        android:text="Add to Animals"
        android:textColor="#258BE2"
        android:textStyle="bold" />

</RelativeLayout>

or instead of inserting a drawable to the left you can insert ImageView with its right aligned to the left of TextView.

Rishabh Srivastava
  • 3,683
  • 2
  • 30
  • 58