Reputation: 767
I have an actionbar with a menu. When you click the button, the menu shows up and all is well with the world.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/reload"
android:title="@string/reload"
android:alphabeticShortcut="r"
android:numericShortcut="1"
android:titleCondensed="Rload"></item>
<group android:id="@+id/adv" android:menuCategory="secondary">
...options
</group>
</menu>
However, I just got told that we have a client whose device has some sort of other menu bar that is displayed over the current one so they can't access the menu. So I added a button on the webview with a javascriptinterface to tell my Activity to display the menu programmatically. I can get there just fine, but I can't for the life of me figure out how to display the menu as if the menu button was pressed.
I tried doing something like this:
private void showMenu() {
try{
actionbar.show();
MenuItem menu = (MenuItem) findViewById(R.id.adv);
menu.expandActionView();
}
catch(Exception e){
Log.e(LOG_CAT,"Unable to inflate menu");
}
}
but I'm getting a null pointer exception
java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.view.MenuItem.expandActionView()' on a null object reference
It seems like what I'm trying to do should be trivially easy, but it may be that I'm just doing something boneheaded instead.
EDIT I don't actually want to just show R.id.adv, I would like to show the whole menu so that should probably be findViewById(R.menu.menu_main)
instead, where menu_main
is the name of the file that the above xml is defined in.
Upvotes: 1
Views: 657
Reputation: 44118
findViewById
looks in the view hierarchy of the activity, not the menu. You need to get the items you need right after your menu is created:
Edit:
Didn't think it's possible but it is. With the help of this answer here's how you can do it:
View mOverflow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final View decor = getWindow().getDecorView();
decor.post(new Runnable() {
@Override
public void run() {
ArrayList<View> results = new ArrayList<>();
decor.findViewsWithText(results, "OVERFLOW",
View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
if (results.size() == 1) {
mOverflow = results.get(0);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
private void showMenu() {
actionbar.show();
mOverflow.performClick();
}
And in your styles.xml
:
<style name="AppTheme" parent="android:Theme.Holo">
<item name="android:actionOverflowButtonStyle">@style/Widget.ActionButton.Overflow</item>
</style>
<style name="Widget.ActionButton.Overflow" parent="@android:style/Widget.Holo.ActionButton.Overflow">
<item name="android:contentDescription">OVERFLOW</item>
</style>
findViewsWithText
requires API 14+, however the answer I linked, has a solution for lower APIs.
Upvotes: 2