I need to get a reference to a MenuItem
's corresponding View
without specifying a custom one. Using findViewById()
with a menu item's ID returns null when called from Activity#onCreate()
or onCreateOptionsMenu()
. However, as a few answers to other questions point out, findViewById()
does return the view when called from onOptionsItemSelected()
or from a runnable posted in Activity#onCreate()
, like so...
new Handler().post(new Runnable() {
@Override
public void run() {
findViewById(R.id.some_view);
}
});
So it seems that at some point, Android takes the Menu
that you populate in onCreateOptionsMenu()
and creates the corresponding View
s that are shown on screen. A Runnable
posted from onCreate()
happens to get run after that happens. But it feels so hacky to assume that will always be the case instead of actually listening for some kind of menuViewsCreated()
event.
So that brings me to my question – does Android provide an event that tells developers that the menu View
s have been created?