Android don't want to see the OptionMenu - android

I have added a button in the action bar, it works fine but if I press the physical menu button, the menu appears action_settings. Instead I want to use only the menu in the ActionBar and not that of the physical menu button.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
getActivity().getMenuInflater().inflate(R.menu.menu_scegli, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_c:
Intent intent = null;
intent = new Intent(getActivity(), Inserisci_.class);
startActivity(intent);
return true;
case R.id.action_settings:
return false;
default:
return super.onOptionsItemSelected(item);
}
}

You can try overriding onKeyDown method in your activity and returning true if the menu key was pressed - this will effectively prevent android from internally handling the menu key:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_MENU) {
return true;
}
else {
return false;
}
}

Related

Fragment Menuitem Click

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_fragement, menu);
super.onCreateOptionsMenu(menu, inflater);
}
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent = new Intent();
switch (item.getItemId()) {
case R.id.color_menu:
intent.setClass(rootView.getContext(), CandleColorActivity.class);
getActivity().startActivityForResult(intent,COLOR_ACTION);
break;
}
return super.onOptionsItemSelected(item);
}
Using the above code menu item is visible in the fragment but item click is not working. Menu item xml:
<item
android:title="selectColor"
android:icon="#drawable/addcolor"
app:showAsAction="always"
android:id="#+id/color_menu"></item>
main activity menu xml always show on each of the fragment
<item
android:title="menu"
android:icon="#drawable/menu"
app:showAsAction="always"
android:id="#+id/uper_menu"></item>
Main activity menu java code open a dialog box on the item click
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.uper_menu:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(R.layout.menu_dialog);
alertDialog = builder.show();
alertDialog.getWindow().setGravity(Gravity.BOTTOM);
alertDialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
alertDialog.getWindow().getDecorView().setBackgroundResource(android.R.color.transparent);
viewIds();
break;
default:
break;
}
return true;
}
You should not call the super class' onOptionsItemSelected(), if you handled the event yourself. So change your method to this:
public boolean onOptionsItemSelected(MenuItem item) {
...
switch (item.getItemId()) {
case R.id.color_menu:
...
return true;
default:
return super.onOptionsItemSelected(item);
}
}
EDIT
In fragment and activity, only return true, if you handled the event, otherwise return super.onOptionsItemSelected(item);
Reason is, that the system first asks the activity to handle the event, and if the activity says it did handle it (by returning true), the system doesn't ask the fragment anymore.

Handling Click Event for menu Used for Floating Action Button Options

I used a library recently that Provides a Floating Action Button and changing to a Something like Navigation bar by clicking on it.
but i cant set click Event for my navigation bar Items.
after Pressing on items i don't see any reaction!
Anyone can help me?
fabOptions = (FabOptions) findViewById(R.id.fabcontainer);
fabOptions.setButtonsMenu(R.menu.menu1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu1, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.back:
newBack();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void newBack() {
Toast.makeText(this, "back", Toast.LENGTH_SHORT).show();
}
}
First of all you should have to use break whenever you use switch statement otherwise the program will go to default status. so use break; the code below.
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getId(); // Retrieve the id of the selected menu item
switch(id) {
case R.id.back:
newBack();
return true;
break;
case ...
}
return super.onOptionsItemSelected(item);
}

How to disable actionbar back button in ActionMode

I use ActionMode to edit some content, those three buttons on the right do everything needed, and the back/home/up button on the left becomes redundant.
Here's the ActionMode
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.editormenu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.action_cancel:
selectedView.onCancelEditing();
selectedView.enableEditMode(selectedView,false);
mode.finish();
return true;
case R.id.action_remove:
selectedView.enableEditMode(selectedView,false);
relContainer.removeView(selectedView);
mode.finish();
return true;
case R.id.action_done:
selectedView.enableEditMode(selectedView,false);
mode.finish();
return true;
default:
return false;
}
}
#Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};
I tried to use that back/home/up button as "cancel" button but it seems that it's id is not android.R.id.home nor R.id.home
so question is: How do I remove/disable this button, OR How do I make use of it?
Just call dispatchKeyEvent method and perform action on it:
Have a look on the following example:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(mActionModeIsActive) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
// handle your back button code here
return true; // consumes the back key event - ActionMode is not finished
}
}
return super.dispatchKeyEvent(event);
}
For further info:
Prevent to cancel Action Mode by press back button
This answer is what worked for me:
remove back/home button from action mode on long press in android
I added these lines to my styles.xml
<item name="actionModeCloseDrawable">#color/colorAccent</item>
<item name="actionModeCloseButtonStyle">#style/Widget.AppCompat.ActionMode</item>
<item name="actionModeBackground">#color/colorAccent</item>
I am using #color/colorAccent as the background colour for the ActionBar in ActionMode.

Hide Action bar onCreateOptionMenu icon for some activites and fragment android

A sliding menu was implemented using google navigation drawer with actionbar class. My problem is onCreateOptionsMenu is being shown in every activities. how can i make onCreateOptionsMenu icon visible and invisible at will. Any idea please.
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.layout.menu, menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.settings:
Intent i=new Intent(class1.this, clas2.class);
startActivity(i);
return true;
}
return false;
}
Either you create menu runtime or have different menu layouts as per your need and in onCreateOptionsMenu of your activity set that layout or runtime create those menus or if you want to show no menu icons then just do menu.clear()
Activity A
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.layout.menu_a, menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.settings:
Intent i=new Intent(class1.this, clas2.class);
startActivity(i);
return true;
}
return false;
}
Activity B
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
menu.clear();
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.layout.menu_b, menu);
// OR
Drawable tmpDrawable = getResources().getDrawable(R.drawable.share_sharingicon);
// tmpDrawable.setColorFilter(getResources().getColor(R.color.colorGrayFont), PorterDuff.Mode.MULTIPLY);
menu.add("ShareMap").setIcon(tmpDrawable).setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add("Directions").setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.ShareMap:
Intent i=new Intent(class1.this, clas2.class);
startActivity(i);
return true;
case R.id.Directions:
Intent i=new Intent(class1.this, clas2.class);
startActivity(i);
return true;
}
return false;
}
To hide or show some icon on action bar, you need override method:
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
MenuItem your_icon = menu.findItem(R.id.action_your_icon);
//show icon
your_icon.setvisible(true);
//hide icon
your_icon.setvisible(false);
...
}
Besides you need 'supportInvalidateOptionsMenu()' to Invalidate the activity's options menu when action bar items have some change

Menu doesn't appear after linking to another page with phonegap

I use phonegap,
When linking to another page with simple href my menu stop working.
In my Java class I override onKeyDown() and return false for menu button
#Override
public boolean onKeyDown(int i,KeyEvent e){
if (i==KeyEvent.KEYCODE_MENU){
return false;
}else{
return true;
}
}
When I loadUrl from the java class I can open the menu (in about.html)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.exit:
this.finish();
return true;
case R.id.about:
super.loadUrl("file:///android_asset/www/about.html");
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Categories

Resources