Search code examples
androidxmlandroid-layoutandroid-coordinatorlayoutcoordinator-layout

How can I achieve this layout with a CoordinatorLayout, EditText, and ListView


I am switching over to a CoordinatorLayout from a RelativeLayout so I can take advantage of the FAB and moving with a Snackbar. My issue is that I was able to use layout_below to give hierarchy and order to the items within my layout. But, when I ran the app with a CoordinatorLayout, everything seems to go haywire and overlap. I tried adding padding, but would rather use layout_below to guarantee that everything will function 100%.

What it looked like with RelativeLayout priorWhat it looks like with CoordinatorLayout

And wrapping some elements in a RelativeLayout doesn't seem to help at all.

 <?xml version="1.0" encoding="utf-8"?>
   <androidx.coordinatorlayout.widget.CoordinatorLayout 
     xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relative"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/default_gap"
        android:paddingBottom="20dp"
        android:orientation="horizontal">
        <EditText
            android:id="@+id/input"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="15dp"
            android:layout_weight="1"
            android:ems="10"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:focusedByDefault="true"
            android:hint="@string/input_a_number"
            android:imeOptions="actionDone|flagNoPersonalizedLearning"
            android:importantForAutofill="no"
            android:inputType="numberSigned|numberDecimal"
            android:maxLength="30"
            android:maxLines="1"
            android:singleLine="true"
            tools:ignore="LabelFor"
            tools:targetApi="o" />
        <Spinner
            android:id="@+id/spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="@dimen/default_gap"
            android:layout_weight="1"
            android:minWidth="180dp" />
    </LinearLayout>

    <LinearLayout
        android:paddingTop="20dp"
        android:layout_below="@id/linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/linearParent">
    <ListView
        android:id="@+id/listview"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="5dp"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="10dp" />
    </LinearLayout>

    [![<!--        android:nestedScrollingEnabled="true"-->][1]][1]

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="16dp"
        app:fabSize="auto"
        android:scaleType="fitXY"
        app:borderWidth="0dp"
        app:useCompatPadding="true"
        android:visibility="gone"
        app:backgroundTint="@color/jade_fab"
        app:elevation="4dp"
        app:srcCompat="@drawable/jade" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Solution

  • Replace your xml file with this layout file. It should do the job!

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/relative"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
        <LinearLayout
            android:id="@+id/linear"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/default_gap"
            android:paddingBottom="20dp"
            android:orientation="horizontal">
            <EditText
                android:id="@+id/input"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="15dp"
                android:layout_weight="1"
                android:ems="10"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:focusedByDefault="true"
                android:hint="@string/input_a_number"
                android:imeOptions="actionDone|flagNoPersonalizedLearning"
                android:importantForAutofill="no"
                android:inputType="numberSigned|numberDecimal"
                android:maxLength="30"
                android:maxLines="1"
                android:singleLine="true"
                tools:ignore="LabelFor"
                tools:targetApi="o" />
            <Spinner
                android:id="@+id/spinner"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="@dimen/default_gap"
                android:layout_weight="1"
                android:minWidth="180dp" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_marginTop="20dp"
            android:layout_below="@id/linear"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/linearParent">
            <ListView
                android:id="@+id/listview"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="5dp"
                android:layout_marginTop="5dp"
                android:layout_marginEnd="10dp" />
        </LinearLayout>
    
        </LinearLayout>
    
        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="16dp"
            app:fabSize="auto"
            android:scaleType="fitXY"
            app:borderWidth="0dp"
            app:useCompatPadding="true"
            android:visibility="gone"
            app:backgroundTint="@color/jade_fab"
            app:elevation="4dp"
            app:srcCompat="@drawable/jade" />
    
    </androidx.coordinatorlayout.widget.CoordinatorLayout>