Andrew
Andrew

Reputation: 4702

Android: Detect opened keyboard, onApplyWindowListener not working

I am trying to hide one specific button at the bottom of my layout when the keyboard is opened, in order to make more view available for the user.

With the release of androidx.core:core-ktx:1.5.0-alpha02 google (finally) added a method called insets.isVisible(WindowInsetsCompat.Type.ime()) which returns a boolean whether the keyboard is opened or clicked.

I am using a base class EmailFragment where I set the function in order to achieve the above written functionality. My problem is that my ViewCompat.setOnApplyWindowInsetsListener(view) gets never called (no toast etc).

I've also tried to set ViewCompat.setOnApplyWindowInsetsListener(view) directly in the used Fragments, but that changed nothing.

My min API is 21, in my AndroidManifest.XML I have android:windowSoftInputMode = adjustResize

Code

abstract class EmailFragment<out T: ViewDataBinding>(
    layout: Int,
    // ... some other stuff that is not necessary for the question
) : BaseFragment<T>(layout) {
    // ... some other stuff that is not necesarry for the question

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        hideButton(view)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    
    private fun hideButton(view: View) {
        ViewCompat.setOnApplyWindowInsetsListener(view) { v, insets ->
            val isKeyboardVisible = insets.isVisible(WindowInsetsCompat.Type.ime())
            if (isKeyboardVisible) {
                btn.visibility = View.GONE
                Toast.makeText(requireContext(), "KEYBOARD OPEN", Toast.LENGTH_SHORT).show()
            } else {
                btn.visibility = View.VISIBLE
                Toast.makeText(requireContext(), "KEYBOARD CLOSED", Toast.LENGTH_SHORT).show()
            }

            // Return the insets to keep going down this event to the view hierarchy
            insets
        }
    }
}

Fragment that inherits from EmailFragment (one out of five)

class CalibrateRepairMessageFragment(
    //... some other stuff that is not necessary for this question
) : EmailFragment<FragmentCalibrateRepairMessageBinding>(
    R.layout.fragment_calibrate_repair_message,
    //... some other stuff that is not necessary for this question
) {
    //... some other stuff that is not necessary for this question

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        //... some other stuff that is not necessary for this question
    }

Screenshot 1 (censored)

enter image description here

Screenshot 2, not working (censored)

enter image description here

I know that using android:windowSoftInputMode = adjustPen makes my button "invisible" but then I cannot scroll anymore, big sad..

Another solution could be that the keyboard just overlaps the button, but I have no clue how to do that...

I appreciate every help, thank you.

Upvotes: 8

Views: 3778

Answers (1)

MidasLefko
MidasLefko

Reputation: 4549

I was able to get this to work by:

  1. Adding android:windowSoftInputMode="adjustResize" to Activity tag in manifest
  2. Setting OnApplyWindowInsetsListener on window.decorView.

However, this had unfortunate side effects to the statusbar which means that OP's comment ("I have no hope that this will ever work") is probably accurate. Hopefully it will work when it graduates from alpha and beta.

Upvotes: 3

Related Questions