2

Hello,

I need to put a view (a ViewPager with a RecyclerView inside) behind my keyboard, so this view will be visible when keyboard is hidden.

My view needs also to have the same height of the keyboard. To get that height I use this method.

For information, I'm in a fragment

I've tried many SoftInputMode but each have problems :

  • adjustResize/adjustUnspecified : The view is above of keyboard and not behind
  • adjustPan : I get the height of keyboard but without the "autocorrection" part, and it breaks the RecyclerView behavior
  • adjustNothing : Doesn't get the height of keyboard

I've also tried to change the SoftInputMode programmatically, but it doesn't work either.

I also sometimes strange behaviors like :

  • The view have height of keyboard without "autocorrection" part, but height change correctly when I change viewpagers's tab
  • Sometimes the RecyclerView inside my view doesn't work correctly (I have to scroll Recycler to update him)

Below some part of my code :

AndroidManifest

android:windowSoftInputMode="stateHidden|adjustUnspecified"

onViewCreated

// I store the height of Keyboard in a custom object Utilities,
// to avoid to instantiate KeyboardHeightProvider
// if I go in this fragment multiple times
if (Utilities.keyboardHeight > 0) { 
    (viewPager?.layoutParams as ConstraintLayout.LayoutParams).height = Utilities.keyboardHeight
    viewPager?.requestLayout()
    activity?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
} else {
    keyboardHeightProvider = KeyboardHeightProvider(activity!!)
    textLayout.post { keyboardHeightProvider?.start() }
}

onKeyBoardHeightChanged(height: Int)

if (Utilities.keyboardHeight < height) {
    Utilities.keyboardHeight = height
    (viewPager?.layoutParams as ConstraintLayout.LayoutParams).height = Utilities.keyboardHeight
    viewPager?.requestLayout()
    activity?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
}

onDestroy()

super.onDestroy()
keyboardHeightProvider?.close()
activity?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED + WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)

I'm sorry for my bad english, Thank you for your time and your help

EDIT

I've found a solution ! With this setting in my manifest

android:windowSoftInputMode="stateHidden|adjustUnspecified"

I change the softInputState in the onViewCreated with this line

activity?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING)

and I restore the previous SoftInputMode in the onDestroy

I don't know how it works because if I set adjustNothing directly in the Manifest, it doesn't get the height of keyboard.

Hope it can help someone

Ady
  • 230
  • 3
  • 16
  • Have you seen this? Did it help? https://stackoverflow.com/questions/16788959/is-there-any-way-in-android-to-get-the-height-of-virtual-keyboard-of-device – Torben Aug 21 '19 at 10:08
  • @Torben Yes I've seen this post, is from it that I get my method ```onKeyBoardHeightChanged``` – Ady Aug 21 '19 at 11:57

0 Answers0