Last active
July 31, 2020 06:20
-
-
Save Kiolk/92fc710ccdf93c40a604a7a396245ae7 to your computer and use it in GitHub Desktop.
Popup menu with icons
This file contains hidden or 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
private fun showPopupMenu() { | |
val wrapper = ContextThemeWrapper(requireContext(), R.style.AppTheme) | |
val popup = PopupMenu(wrapper, ANCHOR_VIEW) | |
try { | |
val fields = popup::class.java.declaredFields | |
fields.forEach { | |
if ("mPopup" == it.name) { | |
it.isAccessible = true | |
val helper = it.get(popup) | |
val classPopupHelper = Class.forName(helper::class.java.name) | |
val method = classPopupHelper.getMethod("setForceShowIcon", Boolean::class.java) | |
method.invoke(helper, true) | |
return@forEach | |
} | |
} | |
} catch (exception: Exception) { | |
Timber.e(exception) | |
} | |
popup.menuInflater.inflate(R.menu.YOUR_MENU_WITH_ICONS, popup.menu) | |
popup.setOnMenuItemClickListener { | |
when (it.itemId) { | |
//TODO logic for handle click on menu items | |
} | |
true | |
} | |
popup.show() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment