Android RelativeLayout alignTop doesn't work

10,291

The problem may be in your image or background. I tested with another image and both are aligned along the tops.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.coapps.pico"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

android:orientation="vertical" >

<!-- title layout -->

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="wrap_content"
    android:layout_height="200dp"
    android:background="@drawable/background_fragment_title"
    >

    <!-- profile picture -->

    <ImageView
        android:id="@+id/fragment_home_profile_picture"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:src="@drawable/falcao_large"
        android:contentDescription="@string/app_name"
        android:scaleType="fitXY"
        />

    <!-- user name -->

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/fragment_home_profile_picture"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/fragment_home_profile_picture"
        android:gravity="top"
        android:textColor="@android:color/white"
        android:text="Roi Mozer"

        android:textSize="30sp"/>

</RelativeLayout>

enter image description here

I think problem is image background. The text align to top of background

Share:
10,291
Asaf Nevo
Author by

Asaf Nevo

Current CEO @Pico - Converting Events into Engagement and the company's former CTO. Experience in developing variety of different environment including DB, server side and client side. Done SQL, C#, PHP, Java, Android, IOS, Web.. Love the StackOverflow community and use it on a daily basis

Updated on June 04, 2022

Comments

  • Asaf Nevo
    Asaf Nevo almost 2 years

    I'm trying to use alignTop attribute in a RelativeLayout in order to set a text to right of a profile picture - aligned to picture's top.

    This is my layout:

    <?xml version="1.0" encoding="utf-8"?>
    <!-- Home fragment layout -->
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res/com.coapps.pico"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/background_dark_green"
        android:orientation="vertical" >
    
        <!-- title layout -->
    
        <RelativeLayout
            android:id="@+id/relativeLayout1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/background_fragment_title" >
    
            <!-- profile picture -->
    
            <ImageView
                android:id="@+id/fragment_home_profile_picture"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_centerVertical="true"
                android:layout_marginLeft="10dp"
                android:background="@drawable/background_profile_picture"
                android:contentDescription="@string/app_name"
                android:scaleType="fitXY"
                android:src="@drawable/test_pic" />
    
            <!-- user name -->
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/fragment_home_profile_picture"
                android:layout_marginLeft="10dp"
                android:layout_toRightOf="@+id/fragment_home_profile_picture"
                android:gravity="top"
                android:text="Roi Mozer"
                android:textColor="@android:color/white"
                android:textSize="30sp"/>
    
        </RelativeLayout>
    
    </RelativeLayout>
    

    This is how it looks in the preview (and the same in the phone):

    As you can see the name center is aligned to picture's top - don't know why.. enter image description here

    How can i set them both to be in the same line ?

    UPDATE: When i changed the layout height to a given height (and not wrap content) it does work...