Question

I need to place some text under an image. With my current code, the text is under the image, but with a fairly large top margin, and is off-center from the image (moved to the right about 10dp). I would like to have my text directly below the image, centered with the image.

I've tried android:layout_centerInParent="true" but that only places my text in the middle of the screen.

Here's my code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ScanResults" >

<ImageView
    android:id="@+id/productLogo"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@drawable/wedge_color"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" />

<TextView
    android:id="@+id/productTitle"
    android:layout_width="140dp"
    android:layout_height="wrap_content"
    android:layout_below="@id/productLogo"
    android:gravity="center"
    android:text="Fluffer Nutter Sandwich"
    android:textSize="14sp" />

</RelativeLayout>

Thanks for any help.

Was it helpful?

Solution

For ImageView, you were giving "100dp" to android:layout_width but in case of TextView, you were giving 140dp...that's why the TextView was not aligning correctly. Now set android:layout_width="100dp" for TextView, the problem will be solved.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ScanResults" >

    <ImageView
        android:id="@+id/productLogo"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/wedge_color"
        android:paddingLeft="10dp"
        android:paddingRight="10dp" />

    <TextView
        android:id="@+id/productTitle"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/productLogo"
        android:gravity="center"
        android:text="Fluffer Nutter Sandwich"
        android:textSize="14sp" />

</RelativeLayout>

OTHER TIPS

add android:gravity="center_horizontal|center_vertical" to the textview to make it center to the layout.

<TextView
    android:id="@+id/productTitle"
    android:layout_width="140dp"
    android:layout_height="wrap_content"
    android:layout_below="@id/productLogo"
    android:gravity="center_horizontal|center_vertical"
    android:text="Fluffer Nutter Sandwich"
    android:textSize="14sp" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top