Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide navigation bar when spinner open

Tags:

android

I was able to set Immersive mode in my app that properly hides navigation and status bars in almost every case. The only exclusion I found so far is, that when I tap on a Spinner component that has android:spinnerMode="dropdown", the navigation bar still comes up. It disappears after I select an item from the dropdown, but I want it to not show up at all. Is there a way I could do this?

like image 673
Anchal Avatar asked Oct 30 '22 13:10

Anchal


1 Answers

I was able to fix this, although this didn't fit my needs well enough, it still prevents a window focus change, which is what causes the system UI to show up. Sorry it's in Kotlin

In your activity's onCreate()

try {
    val popup = Spinner::class.java.getDeclaredField("mPopup")
    popup.isAccessible = true

    // Get private mPopup member variable and try cast to ListPopupWindow
    val popupWindow = popup.get(yourSpinnerView) as android.widget.ListPopupWindow

    popupWindow.isModal = false
} catch (e: Throwable) {
    // silently fail...
}

The important part is this line:

popupWindow.isModal = false

This doesn't affect the spinner interactions, at least for me it still registers clicks properly and hides when a tap occurs outside the popup. However, it doesn't capture all taps, so touching other screen elements will trigger their onClickListener as well as hide the spinner. Because of that, I've decided to not use this method and just deal with the Nav bar appearing, since I couldn't get what I wanted by using this.

Hope this helps others, as it took me the better part of a week to figure this out. I wish Android didn't consider opening a spinner as a window visibility change, removing all window flags and killing immersive mode.

EDIT: Removed the custom class, opting for the simplest solution which is very easy to implement.

like image 175
Mick Ashton Avatar answered Nov 15 '22 07:11

Mick Ashton