Disable menu button? - android

I have a custom menu options that I want to disable it from popping up if a button on screen is clicked..
I thought of using this code but it doesnt work:
#Override
public boolean onPrepareOptionsMenu (Menu menu) {
if (Schedule)
menu.getItem(1).setVisible(View.GONE);
return true;
}
Is there a way to prevent the menu button from doing anything? Thanks.

According to the documentation:
You must return true for the menu to be displayed; if you return false it will not be shown.
So I'm guessing this will work:
#Override
public boolean onPrepareOptionsMenu (Menu menu) {
.... Code .....
return !Schedule;
}
That is assuming that you want the menu to display when Schedule is equal to false.

Related

How to hide ActionPopupWindow of Text Selection Handler of EditText

Actually I want to hide ActionPopupWindow (popup having SELECT ALL, CLIPBOARD options) when user click on + icon(refer to the attached image).
ActionPopupWindow appears when user click on the Text Selection Handler(bubble) (which appears when user tap on the text in the EditText).
I have tried to use setTextIsSelectable() method of EditText but it is not working consistently.
Any help or guidance will be well appreciated.
UPDATE: To hide the Popup already opened and showing on the screen, you need to clear focus of the current EditText or focus on other view when you clicked the plus button. See the
example below:
iconPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
yourEditText.clearFocus();
}
});
If you want the popup never shows up at the first place, there are many ways to do it.
The simplest way is disabling long click and selection feature:
yourEditText.setLongClickable(false);
yourEditText.setTextIsSelectable(false);
Second one is overriding action callback actions on your edittext:
yourEditText.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
public void onDestroyActionMode(ActionMode mode) {
}
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
});
You can use them separately or together according to your case.
Also you can check other options from here

Hide CAB on EditText long press showing the Selection Markers

First of all, I have searched and tried numerous solutions posted here in StackOverflow but none of them solves my query, so this isn't a duplicate question.
I want to display a custom menu like this, when an EditText is long pressed,
As you can see that on Long Press on the EditText, this floating menu appears instead of the stock CAB, and the selection markers are also intact.
I want to achieve this kind of menu for my app. I have tried using this code, while it disabled the CAB but also prevents the selection markers from appearing,
editText.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
});
Also, if I try customizing the CAB menu, it won't render a menu like this, it will just alter the buttons on the existing CAB.
How can I achieve this floating menu along with the selection markers visible, when the EditText is long pressed or double tapped?
Perhaps you can use EditText's getSelectionStart and getSelectionEnd when the OS ActionMode starts (by overriding Activity's onActionModeStarted), then close the OS ActionMode and start your own while manually highlighting the appropriate text using EditText's setSelection.

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;
}

onCreateOptionsMenu not working properly

I have this recurring issue with the onCreateOptionsMenu method. I have it set up so it enables or disables options depending on the value of some SharedPreferences, but for some reason the first time you open the menu it doesn't work as it should, the options that should be disabled are enabled and the other way around. If I close it and reopen it, it works fine.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
menuConfig(menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu (Menu menu) {
menuConfig(menu);
return true;
}
public void menuConfig(Menu menu){
menu.getItem(getResources().getInteger(R.integer.MENU_ABOUT)).setEnabled(true);
if (preferences.getBoolean(getString(R.string.PREFS_STARTED), false)){
menu.getItem(getResources().getInteger(R.integer.MENU_START)).setEnabled(false);
menu.getItem(getResources().getInteger(R.integer.MENU_STOP)).setEnabled(true);
}else{
menu.getItem(getResources().getInteger(R.integer.MENU_START)).setEnabled(true);
menu.getItem(getResources().getInteger(R.integer.MENU_STOP)).setEnabled(false);
}
if(!preferences.getBoolean(getString(R.string.PREFS_STARTED),false) && preferences.getBoolean(getString(R.string.PREFS_FILES_CREATED),false)){
menu.getItem(getResources().getInteger(R.integer.MENU_DELETE)).setEnabled(true);
menu.getItem(getResources().getInteger(R.integer.MENU_SET_ID)).setEnabled(true);
}
else{
menu.getItem(getResources().getInteger(R.integer.MENU_DELETE)).setEnabled(false);
menu.getItem(getResources().getInteger(R.integer.MENU_SET_ID)).setEnabled(false);
}
}
These two pics are taken subsequently with a few seconds between each menu key press.
The first one is wrong, it should look like the second one.
I fixed it by changing the default value returned by the queries to the SharedPreferences object but I have no idea of what is actually happening.

Changing options menu/actionbar menu programatically for 3.0+

In short, here's my question:
Can option menus (shown in the actionbar) be modified programatically on android 3.0+?
I have a wizard-style activity in which I use a ViewFlipper to switch between views, or steps.
The steps are: 1 -> 2 -> 3. Only the second screen (2) has a menu item, while the others don't. I have tried hanging on to the Menu reference (source) and either removing/adding items or just hiding/showing them.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.mMenu = menu;
getMenuInflater().inflate(R.menu.my_menu, menu);
mMenu.getItem(0).setVisible(false);
return super.onCreateOptionsMenu(menu);
}
Switch to the second screen ->
public void showNext(View v) {
if (mVFlipper.getDisplayedChild() < (mVFlipper.getChildCount() - 1)) {
mVFlipper.showNext();
if (mVFlipper.getDisplayedChild() == 1) {
setTitle("Second screen");
mMenu.getItem(0).setVisible(true);
}
}
}
This works fine on 2.2, but fails miserably on 4.1. Starting off with a visible MenuItem and hiding it later works. Starting off with an invisible menu item and showing it later -
There is a bug in Android's MenuItem setVisible that causes problems when turning items back to visible.
In your onCreateOptionsMenu(), add a check to see if the displayed page needs the Menu, if it does, add the MenuItem. Then, call invalidateOptionsMenu() whenever the page changes. That will rebuild the Menu.
I was also struggling with this issue, then I applied a small hack:
menu1.setEnabled(false);
menu1.setTitle("");
Then where you want to visible it again:
menu1.setEnabled(true);
menu1.setTitle("Okay"); //or you can set text according to your given updated values.
Problem fixed by having the MenuItem be visible after onCreateOptionsMenu finishes and then hiding it from a callback called after onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.mMenu = menu;
getMenuInflater().inflate(R.menu.my_menu, menu);
boolean dummyVal = super.onCreateOptionsMenu(menu);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
mMenu.getItem(0).setVisible(true);
} else {
mMenu.getItem(0).setVisible(false);
}
return dummyVal;
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
if (mMenu != null) {
mMenu.getItem(0).setVisible(false);
}
}
}
If anyone has this problem, I recommend trying toadzky's suggestion first: calling "invalidateOptionsMenu()".

Categories

Resources