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;

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final View decor = getWindow().getDecorView();
    decor.post(newRunnable() {
        @Overridepublicvoidrun() {
            ArrayList<View> results = newArrayList<>();
            decor.findViewsWithText(results, "OVERFLOW",
                    View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);

            if (results.size() == 1) {
                mOverflow = results.get(0);
            }
        }
    });
}

@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    returntrue;
}

privatevoidshowMenu() {
    actionbar.show();
    mOverflow.performClick();
}

And in your styles.xml:

<stylename="AppTheme"parent="android:Theme.Holo"><itemname="android:actionOverflowButtonStyle">@style/Widget.ActionButton.Overflow</item></style><stylename="Widget.ActionButton.Overflow"parent="@android:style/Widget.Holo.ActionButton.Overflow"><itemname="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"