How to open menu from a button disabling menu key - java

PRINT SCREEN
I'm willing to block to the user the possibility to open the menu using menu key.
The menu must be opened only from the menu button in my application.
So I deleted this part of code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Now if I press the key button it happens nothing and that's what I wanted.
The problem is that if I press my menu button in app, the menu doesn't show up anymore...
So what do?
I thought to add the
getMenuInflater().inflate(R.menu.main, menu);
in my listener on the menu button but here is the error and I don't understand what to do... any advice?
Thank you!

Do it with Style templates. If SDK < Jelly Bean use style with ActionBar.

You have to add the onCreateOptionsMenu part. But override the onKeyDown event to disable the menu key. Like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
return keyCode == KeyEvent.KEYCODE_MENU || super.onKeyDown(keyCode, event);
}

I am not sure but this should work
Define this variable in your main class (outside all subclasses) -
public class ... extends ... {
boolean menu = false;
...
}
And then whenever you click on menu button on your print screen, do the following -
#Override
public void onClick(View v){
menu = true;
openOptionMenu();
}
And in your onCreateOptionMenu, do this -
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if(menu){
getMenuInflater().inflate(R.menu.main, menu);
}
menu = false;
return true;
}

Related

Why Can't I Call setAlpha on Icon in MenuItem

I have been stuck trying to simply change the transparency of this MenuItem icon for hours... I'm not sure what else to do. I am able to successfully call setEnable() on the icon, which means the problem is not accessing it. It must have something to do with the icon itself. I am using the standard Material theme that comes with the "Basic Activity Template", would a style or theme even prevent me from programmatically changing it? I am calling onPrepareOptionsMenu from a fragment, could that be the issue? Here is my code:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
// top menu (action bar)
inflater.inflate(R.menu.menu_searches, menu);
// bottom menu (the one I am having an issue with)
Menu bottomMenu = toolbarBottom.getMenu();
// the MenuItem I am trying to call "setAlpha()" on
deleteMenuItem = bottomMenu.findItem(R.id.action_delete);
for (int itemIndex = 0; itemIndex < bottomMenu.size(); itemIndex++) {
bottomMenu.getItem(itemIndex).setOnMenuItemClickListener(new
MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
return onOptionsItemSelected(item);
}
});
}
}
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
if (itemsChecked > 0) {
deleteMenuItem.setEnabled(true);
/* I have also tried "delete.getIcon().mutate().setAlpha(255);" -
- but I do not have another instance of the icon so I don't think I need to -
- call "mutate()". It didn't work anyways */
deleteMenuItem.getIcon().setAlpha(255);
} else {
deleteMenuItem.setEnabled(false);
deleteMenuItem.getIcon().setAlpha(5);
}
}
menu.findItem() is laggy and calling it within onPrepareOptionsMenu(Menu menu) produces bad user experience. It's better to get MenuItem object once while creating menu, and then just call setAlpha each time menu occures on screen. You can try
MenuItem mDynamicMenuItem;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
// Get dynamic menu item
mDynamicMenuItem = menu.findItem(R.id.menu_item);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
// Here is just a good place to update item
MenuItem delete = mDynamicMenuItem.findItem(R.id.action_delete);
if (itemsChecked > 0) {
delete.setEnabled(true);
delete.getIcon().setAlpha(255);
} else {
delete.setEnabled(false);
delete.getIcon().mutate().setAlpha(5);
delete.getIcon().setAlpha(5);
}
return true;
}

How to fix Context Menu Bar functions (copy/paste etc) after calling setCustomSelectionActionModeCallback() on EditText?

I'm writing Notepad App in which I've got slider menu showing some text format panel. I toggle view of this panel when user tries to select some text, so I've implemented my menu-toggling code into my EditText's setCustomSelectionActionModeCallback() which looks like this:
private void manageContextMenuBar(EditText editText) {
editText.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return true;
}
// There menu is hidden
public void onDestroyActionMode(ActionMode mode) {
if (findViewById(R.id.sliderMenu).getVisibility() == View.VISIBLE) {
findViewById(R.id.sliderMenu).setVisibility(View.GONE);
}
}
// There menu shows up
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
if (findViewById(R.id.sliderMenu).getVisibility() == View.GONE) {
findViewById(R.id.sliderMenu).setVisibility(View.VISIBLE);
}
return true;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return true;
}
});
}
When I long click on text my format menu shows up, and also software context menu with paste/copy/cut button on it.
The problem is that because of my "Overriding" context menu functions, they stopped working. I can click the buttons, but they doesn't work.
I hope You will understand my problem
Any help will be appreciated :)
You should return false from onActionItemClicked method. This way when you click on those menu items Android uses the default actions.
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
From the onActionItemClicked method Documentation:
Returns: true if this callback handled the event, false if the standard MenuItem invocation should continue.

Open or inflate the Actionbar menu on longclick only - Android

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.
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

Change Android Action Bar Menu Item's Icon on Click

I'm using sherlock action bar.
I have 2 items on the action bar. When the item is chosen (active), I want to change the icon's image.
This is my code on Java
#Override
public boolean onPrepareOptionsMenu (Menu menu){
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menutes, menu);
todaySched=menu.findItem(R.id.todaySched);
if(todaySched.isEnabled()){
todaySched.setIcon(R.drawable.calendarselected);
}
return true;
}
but when I do this the icon become double, and the icon won't change neither.
Can someone help?
Use the on onOptionsItemSelected method
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.todaySched:
// put your code here to change the icon
return true;
default:
return super.onOptionsItemSelected(item);
}
}
you may need to include the correct namespace for the ActionBar Sherlock library to ensure it Overrides the correct menu item. So the start of the method will look like this:
#Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item)

Is there a way to change the text of an Action Bar Menu Item when I touch the menu item in Android?

When I touch this "Edit" button I want it to change to "Done". Is there a way to do this?
I tried this but it only works when opening the menu on the right side:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// change the text here.
return super.onPrepareOptionsMenu(menu);
}
I assume that, at some point, you inflate the menu, yes?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.some_menu_layout, menu);
return true;
}
So you can go into your menu (in this case, res/menu/some_menu_layout.xml), and find the android:id of the menu item you want to find. You should then be able to use menu.findItem(theId).setTitle("Done");. (You may have to save your Menu variable as a local member to do this.)

Categories