metzgore
metzgore

Reputation: 61

How to keep FloatingActionButton and EditText above keyboard at the same time?

I'm currently struggling with my layout and am stuck. I have two EditTexts, the second one can be multi line and I want my layout to adjust when new lines are added and the keyboard is shown. And I also want my FAB to stay above the keyboard all the time. Essentially I want to have the same behavior as in Google Keep when writing a note.

Here are my layouts:

activity_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:layout_scrollFlags="scroll|enterAlways"/>

    </android.support.design.widget.AppBarLayout>

    <!-- app:layout_behavior must be inside CoordinatorLayout (not in fragment) for correct scroll behavior -->
    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/activity_horizontal_margin"
        app:layout_anchor="@id/fragment_container"
        app:layout_anchorGravity="bottom|right|end"/>

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

fragment_note_detail.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <EditText
        android:id="@+id/fragment_note_detail_title_edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/recyclerview_margin"
        android:layout_marginLeft="@dimen/recyclerview_margin"
        android:layout_marginRight="@dimen/recyclerview_margin"
        android:hint="@string/title_hint"
        android:textStyle="bold"
        android:singleLine="true"
        android:imeOptions="actionNext"
        tools:text="Title goes here"/>

    <EditText
        android:id="@+id/fragment_note_detail_description_edittext"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/recyclerview_margin"
        android:hint="@string/description_hint"
        android:gravity="top|start"
        tools:text="Description goes here"/>

</LinearLayout>

When I set

android:windowSoftInputMode="adjustResize"

for the specific activity, then the FAB stays above the keyboard, but the second EditText is overlapped by the keyboard when new lines are added.

When I set

android:windowSoftInputMode="adjustPan"

then the EditText and the cursor are always is above the keyboard, but the keyboard always overlaps the FAB.

How can I have both (FAB and cursor visible at all times)?

Upvotes: 1

Views: 4185

Answers (1)

The Badak
The Badak

Reputation: 2040

You can set 2 or more methods in windowSoftInputMode as written in documentation: Handling Input Method Visibility

<activity android:windowSoftInputMode="stateVisible|adjustResize" ...

Upvotes: 1

Related Questions