Error in using menu item to switch between activities in Android - android

I am trying to use a menu item to switch between activities in my application. Unfortunately, when I tap on the menu item, it does nothing.
Here is the activity.java code:
#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);
return true;
}
// Called when the user selects a contextual menu item
#Override
public boolean onContextItemSelected(MenuItem item) {
//Handles Item Selection.
switch (item.getItemId()) {
case R.id.action_switch_natural:
Intent a = new Intent(this, Natural_Display.class);
startActivity(a);
return true;
default:
return super.onContextItemSelected(item);
}
}
Here is the code for main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_switch_natural"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_switch_natural"/>
</menu>
What am I doing wrong?

onContextItemSelected(MenuItem item) should be onOptionsItemSelected(MenuItem item), as seen here. Don't forget to change super.onContextItemSelected(item) to super.onOptionsItemSelected(item).

I think you have to override onCreatecontextMenu before onContextItemSelected.
try this.
#Override
public void onCreateContextMenu(ContextMenu menu,
View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}

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.

MenuInflater not working - menu not showing

My menu is not showing in the ActionBar (onCreateOptionsMenu) is called properly. The icon and string are available. The code works fine in my other projects. I am using android.support.v7.app.ActionBarActivity for android:
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
Code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
Log.i("onCreate", "menu");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.add, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add:
Intent addIntent = new Intent(this, RoomAddActivity.class);
startActivity(addIntent);
break;
}
return super.onOptionsItemSelected(item);
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_add"
android:icon="#drawable/ic_action_new"
android:showAsAction="always"
android:title="#string/action_add"/>
</menu>
Try to return true explicitly:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
Log.i("onCreate", "menu");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.add, menu);
super.onCreateOptionsMenu(menu);
return true;
}
Updated:
By the way, you are not obliged to call super method. As to my experience it works all right without it.

Menu setActionView(view) to xml

In onCreateOptionsMenu I'm setting an action view with:
MenuItem item = ..
item.setActionView(action_view)
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
and it works ok. But I want to move this to my xml menu and I can't seem to make it show. It only shows up the title. I tried many versions, like:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourApp="http://schemas.android.com/apk/lib/myPackage.myClass" >
<item
android:id="#+id/item"
yourApp:actionViewClass="action_view"
android:showAsAction="always" or youtApp:showAsAction="always"
android:title="title"
</item>
The problem must be it getting to the action_view variable, but I can't seem to figure it out
Did you inflate the menubar?
#Override
public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_name, menu);
mMenu = menu;
if(mMenu != null){
MenuItem item = mMenu.findItem(R.id.item_id);
if(item != null){
item.setVisible(false);
}
}
super.onCreateOptionsMenu(menu, inflater);
}
Also do you have setHasOptionsMenu(true); #onCreate() function?
Try this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item:
// Write your action_view here
break;
case R.id.your_other_item_id:
//Your other Menu item logic
break;
}
return super.onOptionsItemSelected(item);
}

Context Floating Menu

I have created a Context Floating Menu like this:
I added a header too (it is not shown in this picture).
It works perfect but i want to change :
Background
Color/drawable between the header and the first item
The color of the header
And other settings
Can someone show me an example in the styles.xml file how to change some of those settings?
EDIT: To be more specific i will show my code:
Here i register my view for the context menu:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListItemView demoItem1 = (ListItemView) findViewById(R.id.demoItem1);
registerForContextMenu(demoItem1);
}
Here i create and inflate the menu:
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("List Actions");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
Here are the options if clicked:
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.dublicate:
return true;
case R.id.edit:
return true;
case R.id.delete:
return true;
case R.id.rename:
return true;
default:
return super.onContextItemSelected(item);
}
}
Here is the context_menu.xml :
<item
android:id="#+id/dublicate"
android:title="#string/context_menu_item_dublicate">
</item>
<item
android:id="#+id/edit"
android:title="#string/context_menu_item_edit"/>
<item
android:id="#+id/delete"
android:title="#string/context_menu_item_delete"/>
<item
android:id="#+id/rename"
android:title="#string/context_menu_item_rename"/>

OnMenuItemSelected isn't called when layout is set for menu item

I have a menu which is inflated from main_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/act_sync"
android:showAsAction="always"
android:actionLayout="#layout/sync_action"
android:icon="#android:drawable/ic_popup_sync" />
</menu>
and here is the code in the activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
MyMessageHandler.debug("menu item selected");
switch(item.getItemId()){
case R.id.act_sync:
sync();
return true;
}
return super.onOptionsItemSelected(item);
}
But onOptionsItemSelected is not called when I touch the menu item. When I remove the actionLayout attribute of the menu item, it works fine. How can I fix this? Thanks.
you should use below snippet ( Just for reference )
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
final Menu m = menu;
final MenuItem item = menu.findItem(R.id.ActionConnection);
item.getActionView().setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
sync();
}
});
return true;
}
In addition to the answer provided by Vipul Shah.
Make sure you disable the clickable option of all items in your action layout:
android:clickable="false"
Otherwise, it may steal the click so that you still cant receive onClick call back.

Categories

Resources