Skip to content Skip to sidebar Skip to footer

Programmatically Display Actionbar Overflow

I have an actionbar with a menu. When you click the button, the menu shows up and all is well with the world. 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.


Post a Comment for "Programmatically Display Actionbar Overflow"