1

I have two buttons and an EditText. When the keyboard is open, I want the EditText to resize and only one of the two buttons to be above the keyboard.

What I tried was:

  <activity
            android:name=".activities.MyActivity"
            android:exported="false"
            android:windowSoftInputMode="stateVisible|adjustResize"/>

However, this moves both buttons above the keyboard, not only one.

Expected when the keyboard is closed

enter image description here

Expected when the keyboard is opened

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">


    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="0dp"
        app:title="Motivation Setting"
        android:layout_height="56dp"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"/>

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


        android:id="@+id/textInputLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginTop="8dp"
        app:counterEnabled="true"
        app:hintEnabled="true"
        app:layout_constraintBottom_toTopOf="@id/previewButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar">

        <EditText
            android:id="@+id/motivationEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:autofillHints="Motivation Text"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="start|top"
            android:hint="@string/text_reminder"
            android:inputType="textCapSentences|textMultiLine"
            android:minLines="10"
            android:overScrollMode="always"
            android:scrollbarStyle="insideInset"
            android:scrollbars="vertical"
            android:textColorHint="#616161" />
    </com.google.android.material.textfield.TextInputLayout>


    <Button
        android:id="@+id/previewButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="@string/preview"
        app:layout_constraintBottom_toTopOf="@id/submitRequest"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />


    <Button
        android:id="@+id/submitRequest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="@string/submitRequest"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>
TSR
  • 17,242
  • 27
  • 93
  • 197
  • You probably want to use `adjustPan` instead of `adjustResize` - the latter squishes the whole screen as much as it can, so the whole thing is visible. There's some explanation and nice visual examples here: https://stackoverflow.com/q/17410499 If you explicitly want one button visible and one hidden though, I'm not sure if that's possible. Maybe if you do something really clever with an adaptive layout, but that would cause issues on small screens / resized windows. – cactustictacs Jul 31 '22 at 22:01
  • There's also this possibility for Android 11+: https://proandroiddev.com/android-11-creating-an-ime-keyboard-visibility-listener-c390a40d1ad0 Maybe you can hide the bad button when the soft keyboard is visible – cactustictacs Jul 31 '22 at 22:02

0 Answers0