0

I am using a BottomSheetDialogFragment which contains: - A RecyclerView with a list of comments made by users - A EditText at the bottom, where users can write a new comment and post it

When the user taps on the EditText, the keyboard shows up from the bottom. What i want is the keyboard to push ONLY the EditText, so that the user can see what he's typing, but not push the whole BottomSheetDialogFragment.

You can see the desirable behaviour in the Facebook app for example.

I have tried setting different values for setSoftInputMode but all i can achieve is either to move the whole BottomSheetDialogFragment, or to move nothing (leaving the EditText covered).

Gio
  • 190
  • 2
  • 11

1 Answers1

0

The easiest way to do this is to not use a BottomSheetDialogFragment but instead to use a regular fragment that covers the entire window or an activity. Fragment or Activity; whichever you prefer doesn't matter as long as you're taking up the whole screen.

When the on-screen keyboard appears, the activity window (by default) resizes to make way for the on-screen keyboard.

Since a BottomSheetDialogFragment has it's gravity set to bottom, when the activity window resizes, it pushes the bottom sheet and all of it's contents upwards.

Here's an example of the simplest layout that can achieve what you're going for

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <View
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="#80000000" />

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

First is a view with a constant height which imitates the dark area outside of a dialog. No matter how the window resizes this will always be 80dp.

Second is the RecyclerView which resizes based on the remaining available space the LinearLayout has. Pay attention to the attribute android:layout_weight.

Third is EditText which has a constant height of wrap_content. Again No matter how the window resizes this will always be the same height. Since the RecyclerView will take up as much space as it can, this EditText will remain at the bottom of the screen.

Once the window resizes due to the on-screen keyboard appearing, the LinearLayout will resize and recalculate it's children's sizes. Since the View and the EditText has a constant height it will have to make the RecyclerView smaller making it appear like the EditText moved upwards.

You don't have to use a LinearLayout to do this, you can achieve the same effect with any other layout. The key point here is to use a layout that takes up the whole screen.

Subaru Tashiro
  • 2,166
  • 1
  • 14
  • 29
  • Thanks for you answer. Then you think it cannot be achieved with a BottomSheet? I had the idea of showing two independent BottomSheets with different values for softInputMode, one for the recyclerview and another for the EditText on top of it... when the keyboard shows up, the first modal will keep its position while the second will get pushed. – Gio Jun 04 '20 at 13:56