1

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

14 Comments

That makes sense to me, but menu.findItem(R.id.adv); is still returning a null object. Looking at the object in the debugger, it seems like the menu object I'm getting in onCreateOptionsMenu is actually the object I want displayed.
@S.Buda are you sure showMenu is not called before the menu is created?
Yes. showMenu is only called when a user presses a certain button, but onCreateOptionsMenu is called at application start.
@S.Buda is your ActionBar hidden by default?
Possibly, within my onCreate function I create the actionbar and then I hide it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.