28

I have an edit text inside a bottom sheet fragment. when the focus come on the edit text the layout goes up . i tried

 android:windowSoftInputMode="adjustNothing"

its work for the parent activity but not for the dialog fragment.

This is my bottom sheet class:

public class CustomBottomSheetDialogFragment extends BottomSheetDialogFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.content_dialog_bottom_sheet, container, false);
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        return v;
    }
}

initial state

enter image description here

when keyboard comes up

enter image description here

i want the layout to always stay on the bottom the keyboard should come above the layout.

check the layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottomSheetLayout"
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@android:color/holo_blue_light"
android:padding="@dimen/activity_vertical_margin"
app:behavior_hideable="true"
app:behavior_peekHeight="60dp"
app:layout_behavior="@string/bottom_sheet_behavior">


<EditText
    android:id="@+id/edt"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="@android:color/white"
    android:padding="10dp" />

<TextView

    android:layout_width="wrap_content"
    android:layout_height="250dp"
    android:layout_below="@+id/edt" />

Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
DKV
  • 1,767
  • 3
  • 28
  • 49

8 Answers8

33

Use this in your Dialog Fragment.

getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

Inside onCreateView like this.

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.dialog_fragment, container);

        //set to adjust screen height automatically, when soft keyboard appears on screen 
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);


        return rootView;
    }

EDIT 1:

I have made some changes with what layout you are using make it apply in your current layout.

Here is layout.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="400dp"
    android:layout_gravity="bottom"
    android:background="@android:color/holo_blue_light"
    android:padding="10dp"
    app:behavior_hideable="true"
    app:behavior_peekHeight="60dp"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:isScrollContainer="false"
        android:scrollbars="vertical">

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

            <EditText
                android:id="@+id/edt"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:background="@android:color/white"
                android:padding="10dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="250dp"
                android:layout_below="@+id/edt" />

        </LinearLayout>


    </ScrollView>

</FrameLayout>

Here is Fragment.

public class TestFragment extends BottomSheetDialogFragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.test4, container, false);
        return v;
    }

EDIT 2:

You can try android:elevation="50dp" property for shadow above Bottom Sheet give a try with that in frame layout.

enter image description here

Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
  • @VV Post your *dialog fragment code* and also screen shot with reproducing exact issue. Show me your `onCreateDialog` method. – Jay Rathod Sep 05 '16 at 12:55
  • @VV Post your *code* and screen shot to get exact issue. Without that nobody can guide you ahead with this issue. – Jay Rathod Sep 05 '16 at 13:17
  • please check the update. let me know you want any more details. – DKV Sep 06 '16 at 04:02
  • @VV Could you show me your what your `layout` *content_dialog_bottom_sheet* contains. – Jay Rathod Sep 06 '16 at 05:20
  • @ jaydroider : please check the layout – DKV Sep 06 '16 at 06:01
  • @VV Check i have edited my answer and use `layout` i have made and `fragment` both replace with yours. check the working screen shot also. – Jay Rathod Sep 06 '16 at 06:47
  • @VV Sry i didn't get you!! which `activity` ? Please explain bit more. – Jay Rathod Sep 06 '16 at 08:01
  • i done what u told me to do. but it is not working. did do miss anything that u have done – DKV Sep 06 '16 at 08:30
  • @VV I have not added anything using *Activity*. I have just changes your *layout* and *fragment* code just replace both. What problem you are facing ? – Jay Rathod Sep 06 '16 at 08:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122727/discussion-between-jaydroider-and-v-v). – Jay Rathod Sep 06 '16 at 09:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122827/discussion-between-v-v-and-jaydroider). – DKV Sep 07 '16 at 12:41
  • @jaydroider Any care we have to take care if we are using the above content inside CoordinatorLayout with AppBarLayout ? we have to replace ScrollView with NestedScrollView? – LOG_TAG Feb 17 '17 at 04:42
  • @jaydroider say for above example if we have 10 edit text full screen content, scroll conflict may occur even though we have locked the BottomSheetDialogFragment behaviour ! – LOG_TAG Feb 17 '17 at 04:45
  • added app:layout_behavior="@string/appbar_scrolling_view_behavior" to content , app:layout_behavior="@string/bottom_sheet_behavior" to CoordinatorLayout , but nothing seems to be working, scrolling doesn't adjust to the keyboard hight!! – LOG_TAG Feb 17 '17 at 07:00
  • @jaydroider any help available for this http://stackoverflow.com/questions/40664858/android-soft-keyboard-overlays-the-edittext-in-recyclerview-in-bottomsheet#comment71737250_40664858 – LOG_TAG Feb 17 '17 at 07:04
  • have solution for custom view? my bottom sheet is not dialog fragment – Ilya Mashin May 18 '19 at 13:09
  • @IliyaMashin Please ask a new Question with exact issue and details will look into it. – Jay Rathod May 18 '19 at 13:44
  • @JayRathodRJ already did https://stackoverflow.com/questions/56199388/bottom-sheet-not-dialog-fragment-open-with-keyboard – Ilya Mashin May 18 '19 at 13:45
17

100% working formula for BottomSheetFragment

Use this in onCreateDialog in BottomSheetFragment

KeyboardUtil(getActivity(), view);

or

For fragment use

new KeyboardUtil(this, findViewById(R.id.fragment_container));

by using this Util class

https://github.com/mikepenz/MaterialDrawer/blob/aa9136fb4f5b3a80460fe5f47213985026d20c88/library/src/main/java/com/mikepenz/materialdrawer/util/KeyboardUtil.java

Credit:Mikepenz

LOG_TAG
  • 19,894
  • 12
  • 72
  • 105
  • What about the enable and disable methods, where are you using them? – Heitor Colangelo Sep 11 '18 at 13:47
  • 1
    this helped me, my colleague recommended your answer. had been looking for this all over the internet. – Karan Sharma Nov 27 '19 at 00:55
  • this worked for me, but I just have to replace these two lines in the class //get screen height and calculate the difference with the useable area from the r int height = contentView.getHeight(); int diff = (int) (height - r.bottom + r.top + decorView.getContext().getResources().getDimension(R.dimen._16dp)); – aliraza12636 Mar 20 '21 at 07:28
5

Seems like there is bug in the older design version. I encountered the same problem, but after I upgraded the design version, adjustnothing can work as expected.

In gradle:

com.android.support:design:26.1.0

In your BottomSheetDialog:

getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
franny zhao
  • 141
  • 1
  • 5
1

Replace your flag from

adjustNothing

to

adjustPan

Check the Official Document

Waqas Ahmed Ansari
  • 1,683
  • 15
  • 30
1

If you have the following problem

enter image description here

solution

      sheetDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

enter image description here

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
0

in the activity declaration in the manifest, put these lines of code:

android:windowSoftInputMode="stateHidden"
android:windowTranslucentNavigation="true"
android:windowTranslucentStatus="true" 
Khalid Taha
  • 3,183
  • 5
  • 27
  • 43
0

Get the decor view of the dialog and set the bottom padding to height 0 or your dialog height.

getDialog.getWindow.getDecorView.setpadding(0,0,0,0);
Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
surya
  • 607
  • 5
  • 18
0

I have faced the same scenario, Instead of EditText I used SearchView.A keyboard was not hidden when BottomSheetDialog goes to the hidden state.

Simply call this search_channel_template.clearFocus();

I will share my code which I was used to hiding the Keyboard.

search_channel_template = (SearchView) bottomSheetDialog.findViewById(R.id.search_channel_template);

txtVw_template_headerBS_down_id.setOnClickListener(new OnSingleClickListener() {
            @Override
            public void onSingleClick(View v) {
                search_channel_template.clearFocus();
                bottomSheetDialog.getBehavior().setState(BottomSheetBehaviorv2.STATE_HIDDEN);
            }
        });
MohanRaj S
  • 1,958
  • 4
  • 30
  • 54