Open or inflate the Actionbar menu on longclick only - Android

985 views Asked by At

I'm trying to make the actionbar menu (onCreateOptionsMenu) open ONLY on a long-click. How would I achieve this? At the moment I have it working for just a single short press/click using the following code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    // TODO: Only onlongclick should the menu be revealed
    getMenuInflater().inflate(R.menu.my_menu_id, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_home:
            open_home();
            return true;
        case R.id.menu_how_to:
            open_how_to();
            return true;
        case R.id.menu_rate:
            open_rate();
            return true;
        case R.id.menu_about:
            open_about();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

I'd like the menu to ONLY open for a long click (sort of like a hidden/trick feature). I've looked into onLongClickListener but can't get it to work. Thanks for your help! Really appreciate it.

1

There are 1 answers

0
pauloz1890 On BEST ANSWER

I ended up figuring this one out myself. There's 2 ways of doing it. Either a context menu or a alert dialog menu. The alert dialog menu allow icons whereas the context menu does not.

See my answer here: Open Actionbar Menu on Longclick - Android