Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 906
Andrew Avatar asked Sep 13 '20 18:09

Andrew


People also ask

How to check keyboard is opened or not in Android?

Android provides no direct way to determine if the keyboard is open, so we have to get a little creative. The View class has a handy method called getWindowVisibleDisplayFrame from which we can retrieve a rectangle which contains the portion of the view visible to the user.


1 Answers

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.

like image 172
MidasLefko Avatar answered Sep 19 '22 21:09

MidasLefko