martinpelant
martinpelant

Reputation: 2981

Prevent OptionsMenu from closing when clicking on checkbox

I wanna show checkbox on top of the screen to allow the user to enable screenLock and I want to show that checkbox only if he opens the optionsMenu. I am able to do this by overridinbg these 2 methods

@Override
public boolean onMenuOpened(int featureId, Menu menu) {
    checkbox.setVisibility(View.VISIBLE);
    return super.onMenuOpened(featureId, menu);
}
@Override
public void onOptionsMenuClosed(Menu menu) {
    checkbox.setVisibility(View.GONE);
    super.onOptionsMenuClosed(menu);
}

However when I touch the checkbox, in order to check it, the menu closes itself and the checkbox disappears before I can check it.

So my question si simple: How can I prevent the options menu from closing when I touch the checkbox?

Upvotes: 1

Views: 1379

Answers (1)

Drak
Drak

Reputation: 86

The Android stock browser shows a TitleBar you can click while the menu is open. So their solution should fit your problem as well.

The TitleBar is shown using WindowManager:

WindowManager manager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

// Add the title bar to the window manager so it can receive touches
// while the menu is up
WindowManager.LayoutParams params
        = new WindowManager.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.TYPE_APPLICATION,
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
        PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP;
manager.addView(mFakeTitleBar, params);

To remove it (when menu is closed, or user clicks it)

manager.removeView(mFakeTitleBar);

Upvotes: 3

Related Questions