Skip to content

Instantly share code, notes, and snippets.

@Kiolk
Last active July 31, 2020 06:20
Show Gist options
  • Save Kiolk/92fc710ccdf93c40a604a7a396245ae7 to your computer and use it in GitHub Desktop.
Save Kiolk/92fc710ccdf93c40a604a7a396245ae7 to your computer and use it in GitHub Desktop.
Popup menu with icons
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