-
-
Save osvalr/6a5fe2c4e7556989de6ebc924130ec73 to your computer and use it in GitHub Desktop.
Avoid taking window focus by Android Spinner's Dropdown to keep setSystemUiVisibility flags (such as Immersive Mode).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.widget.ListPopupWindow; | |
import android.widget.PopupWindow; | |
import android.widget.Spinner; | |
public static void avoidSpinnerDropdownFocus(Spinner spinner) { | |
try { | |
Field listPopupField = Spinner.class.getDeclaredField("mPopup"); | |
listPopupField.setAccessible(true); | |
Object listPopup = listPopupField.get(spinner); | |
if (listPopup instanceof ListPopupWindow) { | |
Field popupField = ListPopupWindow.class.getDeclaredField("mPopup"); | |
popupField.setAccessible(true); | |
Object popup = popupField.get((ListPopupWindow) listPopup); | |
if (popup instanceof PopupWindow) { | |
((PopupWindow) popup).setFocusable(false); | |
} | |
} | |
} catch (NoSuchFieldException e) { | |
e.printStackTrace(); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.widget.ListPopupWindow | |
import android.widget.PopupWindow | |
import android.widget.Spinner | |
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() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment