Jozef Dochan
Jozef Dochan

Reputation: 935

Overflow menu showing on bottom

In my activity when i press three dots, overflow menu shows normally like this enter image description here

But when i long press "Recent apps" button it shows on bottom of screen like this

enter image description here

XML for menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/search_bar_button"
        android:icon="@drawable/ic_search"
        android:title="Search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="ifRoom|collapseActionView" />

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="1"
        app:showAsAction="never"
        android:title="@string/action_settings" />

</menu>

Code for menu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.toolbar_menu, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    searchButton = menu.findItem(R.id.search_bar_button);
    searchView = (android.support.v7.widget.SearchView) searchButton.getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(getApplicationContext(), SearchActivity.class)));

    return true;
}

I am using support Toolbar, and it happens on all versions of android. Also my theme is using NoActionBar is it because of that ?

Upvotes: 1

Views: 630

Answers (2)

GensaGames
GensaGames

Reputation: 5788

You don't have hard ware button. Sure. But calling your "long press Recent app button" makes the same, like Menu hardware call (action of MenuItem). Then you need to override this call. Also examples Here.

Catch the key event inside onKeyDown() and add your action there. Sample:

@Override
public boolean onKeyDown(int keycode, KeyEvent e) {
    switch(keycode) {
        case KeyEvent.KEYCODE_MENU:
            doSomething();
            return true;
    }

    return super.onKeyDown(keycode, e);
}

Upvotes: 1

Mike
Mike

Reputation: 5582

You need to catch the menu key and open the overflow menu. Add this to your Activity or AppCompatActivity

@Override
public boolean onKeyDown(int keycode, KeyEvent e) {
    switch(keycode) {
        case KeyEvent.KEYCODE_MENU:
            mToolbar.showOverflowMenu();
            return true;
    }
    return super.onKeyDown(keycode, e);
}

Upvotes: 2

Related Questions