I have an android app.
I have a spinner called dd1
defined in my mainactivity.xml
.
I have the onItemSelected
method defined in a fragment.
The app runs full screen, i.e. hides the navigation and status bar. Everything is fine. But the moment I touch the spinner, the status bar pops up, and never goes back.
I need to ensure that the status bar remains hidden.
Attempt to solve
I read this SO Question. It already refers to this GIT gist.
In my Fragment, just before the override fun onCreateView
I define:
fun Spinner.avoidDropdownFocus() {
try {
val listPopup = Spinner::class.java
.getDeclaredField("mPopup")
.apply { isAccessible = true }
.get(this)
if (listPopup is ListPopupWindow) {
val popup = ListPopupWindow::class.java
.getDeclaredField("mPopup")
.apply { isAccessible = true }
.get(listPopup)
if (popup is PopupWindow) {
popup.isFocusable = false
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
Then, in the onItemSelected
, I have:
dd1.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
dd1.avoidDropdownFocus()
... // do my thing
}
}
This does not work. I am using Android 10, but on a DJI Remote Control.
Question
How do I force the status bar to remain hidden during the runtime of the app, despite activating the spinner(s)?
Thank you
Please avoid this reflection way.
May be it’s an issue before Android 11 that status bar pops up when spinner opens, ’cause I can reproduce it on Android 9, but can’t on Android 11.
So, I think you can ignore the pop-up issue.
And to avoid status bar remaining visible when spinner closed, hide status bar manually in your activity:
override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
if (hasFocus) {
WindowCompat.getInsetsController(window, window.decorView).apply {
hide(WindowInsetsCompat.Type.statusBars())
// Or instead hide statusBar and navigationBar together if required.
//hide(WindowInsetsCompat.Type.systemBars())
}
}
}
1