0

I have simply EditText and Button below this EditText in my BottomSheetDialogFragment

<ConstraintLayout>
 <EditText/>
 <Button/>
</ConstraintLayout>

And when soft keyboard opens it covers evetything(in my case Button) below EditText.

I'm using windowSoftInputMode="adjustResize" in my Activity.

I've googled a lot and already found similar old questions but no correct answer on them.

soft keyboard is covering bottom sheet dialog

How to adjust size of BottomSheet with Edittext and button below it?

Is this some kind of buggy behavior?

Ruslan
  • 11
  • 1

3 Answers3

1

I found a solution for the same case. You can just increase padding when soft keyboard will be shown. This solution is available for API 19+.

import android.graphics.Rect
import android.os.Build
import android.view.View
import android.view.ViewTreeObserver
import androidx.core.view.updatePaddingRelative

class UnderKeyboardViewElevator(private val decorView: View, private val contentView: View) {

    private var initialPaddingBottom: Int = contentView.paddingBottom

    private var onGlobalLayoutListener = ViewTreeObserver.OnGlobalLayoutListener {
        val r = Rect()
        decorView.getWindowVisibleDisplayFrame(r)

        val height = decorView.context.resources.displayMetrics.heightPixels
        val diff = height - r.bottom

        if (diff != 0) {
            val targetPadding = diff + initialPaddingBottom
            if (contentView.paddingBottom != targetPadding) {
                contentView.updatePaddingRelative(bottom = targetPadding)
            }
        } else {
            if (contentView.paddingBottom != initialPaddingBottom) {
                contentView.updatePaddingRelative(bottom = initialPaddingBottom)
            }
        }
    }

    init {
        if (Build.VERSION.SDK_INT >= 19) {
            decorView.viewTreeObserver.addOnGlobalLayoutListener(onGlobalLayoutListener)
        }
    }

    fun enable() {
        if (Build.VERSION.SDK_INT >= 19) {
            decorView.viewTreeObserver.addOnGlobalLayoutListener(onGlobalLayoutListener)
        }
    }

    fun disable() {
        if (Build.VERSION.SDK_INT >= 19) {
            decorView.viewTreeObserver.removeOnGlobalLayoutListener(onGlobalLayoutListener)
        }
    }
}

Call enable() at onStart() and call disable() at onStop():

    private var elevator: UnderKeyboardViewElevator? = null

    //...

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = BottomSheetDialog(context!!)

        val dialogRoot = View.inflate(context, R.layout.my_layout, null)
        dialog.setContentView(dialogRoot)

        val decorView = activity!!.window!!.decorView
        elevator = UnderKeyboardViewElevator(decorView, dialogRoot)

        return dialog
    }

    override fun onStart() {
        elevator?.enable()
        super.onStart()
    }

    override fun onStop() {
        super.onStop()
        elevator?.disable()
    }

    //...

Hope it will be useful.

0

Surround your root view with a ScrollView, preferably with scrollbars=none. The ScrollView will properly not change anything with your layout except being used to solve this problem.

And then set fitsSystemWindows="true" on the view that you want to make fully shown above the keyboard.

PJain
  • 526
  • 2
  • 14
0

Use this in onCreate:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
kAliert
  • 768
  • 9
  • 21
SHUBHAM
  • 1
  • 1