Search code examples
androidxmlandroid-layoutposition

"layout_toEndOf" not positioning correctly


I want to display an EditText left to a Button. Therefore, i defined the following .xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/coordinator_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        tools:context="de.abelssoft.lowbatterywarner.DataActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/data_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        app:layout_anchor="@id/coordinator_layout"
        app:layout_anchorGravity="bottom"
        android:background="@color/light_grey">

        <EditText android:id="@+id/edit_message"
            android:background="@android:color/white"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textCapSentences|textMultiLine"
            android:maxLength="2000"
            android:maxLines="4"
            android:hint="Nachricht schreiben..." />

        <Button
            android:id="@+id/button_folder"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toEndOf="@id/button_folder"
            android:background="@drawable/folder"
            android:onClick="onFolderClick"/>

    </RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

Although i am using layout_toEndOf, the button centers in the screen. Why is this happening? Has this something to do with the fact, the relative layout is a child of CoordinatorLayout?


Solution

  • Change your RelativeLayout to this:

     <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        app:layout_anchor="@id/coordinator_layout"
        app:layout_anchorGravity="bottom"
        android:background="@color/light_grey">
    
        <EditText android:id="@+id/edit_message"
            android:background="@android:color/white"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textCapSentences|textMultiLine"
            android:maxLength="2000"
            android:maxLines="4"
            android:hint="Nachricht schreiben..." />
    
        <Button
            android:id="@+id/button_folder"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toEndOf="@id/edit_message"
            android:background="@drawable/folder"
            android:onClick="onFolderClick"/>
    
    </RelativeLayout>