Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View binding for menu files?

Is it possible to use View Binding (or Data Binding) in menu resources?

override fun onCreateOptionsMenu(menu: Menu): Boolean {
    menuInflater.inflate(R.menu.menu_main, menu)
    
    // search using ID - can this be replaced by view binding?
    val searchView = menu.findItem(R.id.menu_search).actionView as SearchView
}
like image 373
stefan.at.wpf Avatar asked May 01 '26 07:05

stefan.at.wpf


1 Answers

In most cases, view binding replaces findViewById.

According to the documentations:

View binding is a feature that allows you to more easily write code that interacts with views. Once view binding is enabled in a module, it generates a binding class for each XML layout file present in that module.

Where in menus it uses findItem() and not findViewById() method.

like image 72
ʍѳђઽ૯ท Avatar answered May 03 '26 22:05

ʍѳђઽ૯ท



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!