Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin Android Extensions and Menu

Is there any way to access menu_item_search menu item defined in fragment_photo_gallery layout using synthetic properties instead of using findItem method?

override fun onCreateOptionsMenu(menu: Menu, menuInflater: MenuInflater) {
    super.onCreateOptionsMenu(menu, menuInflater)
    menuInflater.inflate(R.menu.fragment_photo_gallery, menu)

    //is there a way to access searchItem using synthetic properties?
    val searchItem = menu.findItem(R.id.menu_item_search)
}
like image 605
Igor Avatar asked Dec 19 '22 14:12

Igor


1 Answers

MenuInflater serves a fundamentally different purpose than LayoutInflater.

Despite both having "Inflater" part in its name and implementing methods that are named "inflate()", they do completely different things. MenuInflater inflates Menus, where LayoutInflater inflates Views.

Kotlin Android Extensions were created to simplify usage of Android Views, not Android Menus, or anything that has inflate() method.

Long story short - it is not possible to use KAE with Android Menus.

like image 101
rafal Avatar answered Jan 12 '23 14:01

rafal