In one of my fragments," /> In one of my fragments," /> In one of my fragments,"/>

How to hide action Bar in android once SoftKeyboard shows up

46 Views Asked by At

I have seen these answers.

My androidManifest.xml file contains: android:theme="@style/Theme.AppCompat.DayNight.NoActionBar" >

In one of my fragments, i need to call the soft keyboard. As soon as I call the soft keyboard, the action bar appears as well, and is never hidden after that. In that fragment, I have:

view.setOnClickListener {
            val imm = context?.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager?
            imm?.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)

            (activity as AppCompatActivity).supportActionBar?.hide()

        }

I expect this to hide the keyboard if a part of the fragment besides the input area was tapped (which it does), and also hide the action bar (it does not).

In this fragment, there is a button. On click, a new fragment is loaded. In that newly Loaded fragment's onCreateView I have:

(activity as AppCompatActivity).supportActionBar?.hide()

In the same fragment's onResume I have:

override fun onResume() {
        super.onResume()
        (activity as AppCompatActivity).actionBar?.hide()
        (activity as AppCompatActivity).supportActionBar?.hide()

    }

But the action bar persists.

My android SDKs are as follows:

ANDROID_MIN_SDK_VERSION=23
ANDROID_TARGET_SDK_VERSION=33
ANDROID_COMPILE_SDK_VERSION=33
KOTLIN_VERSION = 1.6.10

Question:

How can I force the action bar to stay hidden, even if the Soft keyboard is called?Thank you

1

There are 1 best solutions below

5
Burak On

I believe this method will solve your problem. It basically listens the keyboard visibility and hides action bar if its visible as you see below. Call it on your activity

fun hideActionBarIfKeyboardVisible() {
     ViewCompat.setOnApplyWindowInsetsListener(window.decorView) { view, insets ->
         val imeVisible = insets.isVisible(WindowInsetsCompat.Type.ime())
         
         if(imeVisible){
             supportActionBar?.hide()
         }
         
         ViewCompat.onApplyWindowInsets(view, insets)
     }
 }