Android options menu always closes - android

I would like to know how to prevent the menu bar from closing.
#Override
public void onOptionsMenuClosed(Menu menu) {
}
When the activity starts, I open the menu and want it to stay open.
new Handler().postDelayed(new Runnable() {
public void run() {
openOptionsMenu();
}
}, 1000);

You cannot keep the Options Menu open and it will always act that way. But what you can do is create your own custom menu in a layout.xml and set the visibility to GONE. Then, override the onKeyDown() method and listen for presses of the menu key. If it is pressed, the options menu will be set to open/close (VISIBLE/INVISIBLE) depending on its current state. That way, you can control if the options menu will remain open or not even after touch.

Related

Closing the FAB sub menu on user touch

I am trying to implement an expandable fab, I implemented it and it works as expected i.e when the user taps on fab_main the submenu becomes visible and active, and when the user taps on the fab_main again the sub menu turn invisible and inactive.
What My problem is that I want the submenu to close (in case it is open) when the user taps on screen like for example the current activity
I have Tried using ontouch (which didn't work) and dispatchTouchEvent(which worked but I was not able to use the sub menu buttons
This is my code for how I implemented expandable FAB
fab_main.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (isOpen) {
textView_img_edit.setVisibility(View.INVISIBLE);
textview_bg_edit.setVisibility(View.INVISIBLE);
fab2_bg_edit.startAnimation(fab_close);
fab1_img_edit.startAnimation(fab_close);
fab_main.startAnimation(fab_anti_clock_wise);
fab2_bg_edit.setClickable(false);
fab1_img_edit.setClickable(false);
isOpen = false;
} else {
textView_img_edit.setVisibility(View.VISIBLE);
textview_bg_edit.setVisibility(View.VISIBLE);
fab2_bg_edit.startAnimation(fab_open);
fab1_img_edit.startAnimation(fab_open);
fab_main.startAnimation(fab_clock_wise);
fab2_bg_edit.setClickable(true);
fab1_img_edit.setClickable(true);
isOpen = true;
}
}
});
I used this code in both onTouch and dispatchTouchEvent
if (isOpen) {
textView_img_edit.setVisibility(View.INVISIBLE);
textview_bg_edit.setVisibility(View.INVISIBLE);
fab2_bg_edit.startAnimation(fab_close);
fab1_img_edit.startAnimation(fab_close);
fab_main.startAnimation(fab_anti_clock_wise);
fab2_bg_edit.setClickable(false);
fab1_img_edit.setClickable(false);
isOpen = false;
}
Try setting a onTouchListener on the base layout. You can check for MotionEvent.ACTION_UP and close your fab submenu (if it's open) there.

How to tell Listview Selection Mode transitions

Is there a way to know if a user exitted multiple selection mode either by pressing back or unselecting all items?
You can use the following method to register a MultiChoiceModeListener.
setMultiChoiceModeListener (AbsListView.MultiChoiceModeListener listener)
This can tell you when the user leaves the multichoice mode.
Not sure about unselecting all items. How does user perform this task?
The solution was to implement :
#Override
public void onDestroyActionMode(ActionMode mode) {
...
}
and
#Override
public void onCreateActionMode(ActionMode mode, Menu menu) {
...
}
To detect exiting and entering the multiple selection mode.

hardware menu button override for redirecting to page

is possible overade hardware menu button? When i tap on hardware menu button, appear new layout page, instead of view of submenu? My basic app do not have any setting so only what i want is credits page. I try lot of options. I mean something like this:
#override
public boolean onCreateOptionsMenu() {
setContentView(R.layout.credits); }
Every help would be great.
From option menu You must return true for the menu to be displayed; if you return false it will not be shown.
So you can use
#override
public boolean onCreateOptionsMenu() {
setContentView(R.layout.credits);
return false;
}

how to keep an options menu always on the screen

I need the options menu to be displayed on screen always.. I've written the code to open options menu on the startup of an activity..
#Override
public void onAttachedToWindow() {
openOptionsMenu();
};
But on clicking on another item on the screen, the menu goes down.. I want the menu to be on screen always.. Is there any way to do that?
Please try the following code:
#Override
public void onOptionsMenuClosed(Menu menu) {
openOptionsMenu();
}

How do I programmatically check if the menu of my activity is showing at a particular moment?

An Android device configuration change (for example "slide the hard keyboard back in") will always call PhoneWindow.onConfigurationChanged(), which in turn, will call reopenMenu(). This will cause the menu of the currently running activity to be reopened, in case it is showing.
I have a lock on my menu implemented in my onPrepareOptionsMenu() override. The user must enter a code each time they want to see the menu. I don't want the user to be asked to enter the code again, while the menu is still up just because of a configuration change. Thus, I would like to know, is there any way I can check if the menu of current foreground activity is already showing? Knowing this, I could bypass asking for the access code if the menu is already up.
My custom workaround implementation is to use my own flag menuShowing, which I set in onPrepareOptionsMenu and reset in onOptionsItemSelected and in onKeyDown if the back button is clicked.
EDIT: It appears a screen orientation configuration change does not trigger this behavior. A hard keyboard slide however, does.
Until someone comes up with a nicer 'one call' answer, here is the custom workaround implementation that I mention in the question, with help from Sam's tips, in case someone needs the same functionality:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (showingMenu) {
// The menu button was clicked or the hard keyboard was
// slid open/closed while the menu was already showing
return true;
}
// Otherwise, either the menu was clicked or openOptionsMenu() was called
if (codeEntered) {
// Code was entered and then openOptionsMenu() was called
showingMenu = true;
// Menu will now be shown
return true;
} else {
// The menu button was clicked, ask for code
askForCode();
// Don't show menu yet
return false;
}
}
#Override
public void onOptionsMenuClosed(Menu menu) {
showingMenu = false;
codeEntered = false;
}
private void askForCode() {
codeEntered = getUserInput();
if (codeEntered)
openOptionsMenu();
}
getUserInput() actually occurs with the help of an AlertDialog and an EditText with an attached TextWatcher but the implementation details exceed the scope of this question, unless someone is interested.
In my case it´s
#Override
public void onPanelClosed(int featureId, Menu menu) {
showingMenu = false;
super.onPanelClosed(featureId, menu);
}

Categories

Resources