Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Show BottomSheetDialogFragment above Keyboard

I'm trying to show a BottomSheetDialogFragment with a few EditText fields for the user to enter information. I want to show it directly above the keyboard, but it keeps covering up the contents.

This is what happens when I bring up the BottomSheetDialogFragment, you can see it's selecting Card Number EditText, but covering the other content.

BottomSheetDialogFragment covering up content

Ideally, this is what I'm looking for, you can see both EditTexts, and the padding of the View.

BottomSheetDialogFragment not covering up content

I've tried a lot of solutions revolving around windowSoftInputMode, but nothing seems to work. I've set it to adjustResize for the parent Activity and the actual BottomSheetDialogFragment via

dialog.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)

And I've also tried modifying my layout, changing it from a FrameLayout, to a ScrollView to a CoordinatorLayout to see if that had any effect on the position of the layout, but nothing seems to work.

If anyone knows how to accomplish this, that would be greatly appreciated, thank you.

like image 411
Advice-Dog Avatar asked May 07 '18 22:05

Advice-Dog


People also ask

How do you move the layout up when the soft keyboard is shown Android?

Sometimes, you need to change the layout when the soft keyboard appeared on the screen. You can fix this by adding a line of code into the AndroidManifest. xml file within the relevant activity section.

How do I close the keyboard on Android?

HIDE_IMPLICIT_ONLY, 0); Here pass HIDE_IMPLICIT_ONLY at the position of showFlag and 0 at the position of hiddenFlag . It will forcefully close soft Keyboard.


1 Answers

Maybe a late answer but would probably help.

Nothing else worked for me. But this solution is working

Inside styles:

<style>  // main theme     <item name="bottomSheetDialogTheme">@style/BottomSheetDialogTheme</item>     ........ // rest of the code </style>  <style name="BottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">     <item name="bottomSheetStyle">@style/AppModalStyle</item>     <item name="android:windowIsFloating">false</item>     <item name="android:windowSoftInputMode">adjustResize</item>     <item name="android:statusBarColor">@android:color/transparent</item>  </style> 

Here android:windowIsFloating should be false & android:windowSoftInputMode must be adjustResize

<style name="AppModalStyle" parent="Widget.Design.BottomSheet.Modal">     <item name="android:background">@drawable/rounded_corner_dialog</item> </style> 

Note: This part is optional but if things don't work perfectly then Wrap layout inside NestedScrollView

<androidx.core.widget.NestedScrollView     android:layout_width="match_parent"     android:layout_height="wrap_content">           <--Rest of the layout--> </androidx.core.widget.NestedScrollView> 

On some devices this solution wasn't enough. Adding this to code solved my problem completely.

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {     super.onViewCreated(view, savedInstanceState)     dialog?.setOnShowListener {         val dialog = it as BottomSheetDialog         val bottomSheet = dialog.findViewById<View>(R.id.design_bottom_sheet)         bottomSheet?.let { sheet ->             dialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED             sheet.parent.parent.requestLayout()         }     } } 
like image 67
Muhammad Umair Shafique Avatar answered Oct 10 '22 03:10

Muhammad Umair Shafique