3

I want to have a button that should be sticky at the bottom when Keyboard is open.

Linear layout with a long-form that contains multiple edittext in the vertical orientation.

Button is at the bottom and outside of scrollview

Manifest has a adjustsize property

So when I click any of the edit text, the Button comes up on the top of the Keyboard.

I have even tried android:fitsToSystemWindow=true but nothing works

I want something like that where the button does not come up on the keyboard and button should be sticky at the bottom too. How can I achieve that ?

Here is my XML file

<androidx.constraintlayout.widget.ConstraintLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent" tools:context=".items.ui.AddEditItemFragment">


    <ScrollView android:id="@+id/sv_add_edit_item" android:layout_width="match_parent"
        android:layout_height="0dp" app:layout_constraintBottom_toTopOf="@+id/btn_save"
        app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
            android:orientation="vertical">

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/itemNameInputLayout" android:layout_width="match_parent"
                android:layout_height="wrap_content" android:background="@color/white"
                android:padding="@dimen/default_margin_padding" app:errorEnabled="true"
                app:errorTextAppearance="@style/ErrorText" app:hintTextAppearance="@style/HintText">

                <com.google.android.material.textfield.TextInputEditText
                    style="@style/editTextInputLayout" android:layout_width="match_parent"
                    android:layout_height="wrap_content" android:inputType="textCapWords"
                    android:maxLines="1" />

            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/itemNameInputLayout" android:layout_width="match_parent"
                android:layout_height="wrap_content" android:background="@color/white"
                android:padding="@dimen/default_margin_padding" app:errorEnabled="true"
                app:errorTextAppearance="@style/ErrorText" app:hintTextAppearance="@style/HintText">

                <com.google.android.material.textfield.TextInputEditText
                    style="@style/editTextInputLayout" android:layout_width="match_parent"
                    android:layout_height="wrap_content" android:inputType="textCapWords"
                    android:maxLines="1" />

            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/itemNameInputLayout" android:layout_width="match_parent"
                android:layout_height="wrap_content" android:background="@color/white"
                android:padding="@dimen/default_margin_padding" app:errorEnabled="true"
                app:errorTextAppearance="@style/ErrorText" app:hintTextAppearance="@style/HintText">

                <com.google.android.material.textfield.TextInputEditText
                    style="@style/editTextInputLayout" android:layout_width="match_parent"
                    android:layout_height="wrap_content" android:inputType="textCapWords"
                    android:maxLines="1" />

            </com.google.android.material.textfield.TextInputLayout>


            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/itemNameInputLayout" android:layout_width="match_parent"
                android:layout_height="wrap_content" android:background="@color/white"
                android:padding="@dimen/default_margin_padding" app:errorEnabled="true"
                app:errorTextAppearance="@style/ErrorText" app:hintTextAppearance="@style/HintText">

                <com.google.android.material.textfield.TextInputEditText
                    style="@style/editTextInputLayout" android:layout_width="match_parent"
                    android:layout_height="wrap_content" android:inputType="textCapWords"
                    android:maxLines="1" />

            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/itemNameInputLayout" android:layout_width="match_parent"
                android:layout_height="wrap_content" android:background="@color/white"
                android:padding="@dimen/default_margin_padding" app:errorEnabled="true"
                app:errorTextAppearance="@style/ErrorText" app:hintTextAppearance="@style/HintText">

                <com.google.android.material.textfield.TextInputEditText
                    style="@style/editTextInputLayout" android:layout_width="match_parent"
                    android:layout_height="wrap_content" android:inputType="textCapWords"
                    android:maxLines="1" />

            </com.google.android.material.textfield.TextInputLayout>


            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/itemNameInputLayout" android:layout_width="match_parent"
                android:layout_height="wrap_content" android:background="@color/white"
                android:padding="@dimen/default_margin_padding" app:errorEnabled="true"
                app:errorTextAppearance="@style/ErrorText" app:hintTextAppearance="@style/HintText">

                <com.google.android.material.textfield.TextInputEditText
                    style="@style/editTextInputLayout" android:layout_width="match_parent"
                    android:layout_height="wrap_content" android:inputType="textCapWords"
                    android:maxLines="1" />

            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/itemNameInputLayout" android:layout_width="match_parent"
                android:layout_height="wrap_content" android:background="@color/white"
                android:padding="@dimen/default_margin_padding" app:errorEnabled="true"
                app:errorTextAppearance="@style/ErrorText" app:hintTextAppearance="@style/HintText">

                <com.google.android.material.textfield.TextInputEditText
                    style="@style/editTextInputLayout" android:layout_width="match_parent"
                    android:layout_height="wrap_content" android:inputType="textCapWords"
                    android:maxLines="1" />

            </com.google.android.material.textfield.TextInputLayout>


        </LinearLayout>

    </ScrollView>

    <Button android:id="@+id/btn_save" style="@style/buttonThemeColor"
        android:layout_width="match_parent" android:text="@string/save"
        app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>```

Any help will be appreciated
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
drjansari
  • 306
  • 1
  • 11

2 Answers2

2

One way can be to add global listener to detect whether keyboard is showing or not. And then hide/show your button accordingly.

You can take help from my this answer for the same.

gprathour
  • 14,813
  • 5
  • 66
  • 90
  • Apart from this any other way like from the XML only I can do this Because then it will become hard to handle when I have a custom keyboard – drjansari Nov 22 '19 at 07:43
  • @drjansari I had similar scenario with bottom tabs but couldn't find any other solution, so ended up doing that. Moreover the listener I used was based on height of view rather than any event so it should work for custom keyboard too. – gprathour Nov 22 '19 at 07:50
-1

In your menifest file use adjustPan for windowSoftInputMode rather then adjustResize

<activity android:name=".youractivity"
    android:windowSoftInputMode="adjustPan" />
Hamza Khan
  • 84
  • 2