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
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
}
}
}
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
}
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.
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.
I was able to get this to work by:
android:windowSoftInputMode="adjustResize"
to Activity
tag in manifestOnApplyWindowInsetsListener
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With