I am using a fourth level Active Menu on my app, everything works perfect but when I select one item on the Menu fourth level, the system is not calling onOptionsItemSelected anymore, so I can't get the selected item on my app.
Here is my code
EDIT 1: the dash - in the Id's are making confusion, I am removing them from the code. Sorry for that
public class Main extends FragmentActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// .... some code
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.Level1: {
Toast.makeText(getBaseContext(), "Level 1", Toast.LENGTH_SHORT).show();
break;
}
case R.id.Level2: {
Toast.makeText(getBaseContext(), "Level 2", Toast.LENGTH_SHORT).show();
break;
}
case R.id.Level3: {
Toast.makeText(getBaseContext(), "Level 3", Toast.LENGTH_SHORT).show();
break;
}
case R.id.Level4: {
Toast.makeText(getBaseContext(), "Level 4", Toast.LENGTH_SHORT).show();
break;
}}
return true;
}
and here the XML file main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/Level1"
android:showAsAction="ifRoom"
android:title="#string/Level1">
<menu>
<item android:id="#+id/Level2"
android:title="#string/Level2">
<menu>
<item android:id="#+id/Level3"
android:title="#string/Level3">
<menu>
<item android:id="#+id/Level4"
android:title="#string/Level4"/>
</menu>
</item>
</menu>
</item>
</menu>
</item>
</menu>
I was reading the documentation but it doesn't states a limit for nested sub-menus.
Finally my solution was to append a android:onClick="onOptionsItemSelected" to the fourth level item like:
<menu>
<item android:id="#+id/Level4"
android:onClick="onOptionsItemSelected"
android:title="#string/Level4"/>
</menu>
to force the call to onOptionItemSelected, it works but is there a better solution???
EDIT 2: I just found that this walk-around doesn't works with Android version 4.0.3 or earlier.!!! not even using onMenuItemSelected...!!!
Now I am in problem, please help...!!
Related
Hello guys i want to display action with text and images on action bar, but i only get text in overflow menu
my menu.xml file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_show_ir_list"
android:showAsAction="ifRoom"
android:title="Cancel"/>
<item
android:id="#+id/phone"
android:title="Wowio"
android:icon="#drawable/logo"
android:showAsAction="ifRoom"
/>
<!--android:icon="#drawable/info"-->
<item
android:id="#+id/computer"
android:title="Done"
android:showAsAction="ifRoom"
/>
</menu>
in my main class
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()){
case R.id.phone:
Toast.makeText(getBaseContext(), "You selected About App", Toast.LENGTH_SHORT).show();
break;
case R.id.computer:
Toast.makeText(getBaseContext(), "You selected About Developer", Toast.LENGTH_SHORT).show();
break;
case R.id.action_show_ir_list:
Toast.makeText(getBaseContext(), "Volume Set To Max", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
Here every thing is coming in over flow menu nothing is coming on action bar
I tried every thing on internet and nothing is working
What should i do to show them on ActionBar?
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:[yourapp]="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_add_size"
android:title="#string/menu_add_item"
android:orderInCategory="10"
[yourapp]:showAsAction="always"
android:icon="#android:drawable/ic_menu_add" />
instead of [yourapp] type your app name
In my code there is a menu in which i am using icons. but while running only the icon title appears not the icon. please help me to solve this.In my code there is a menu in which i am using icons. but while running only the icon title appears not the icon. please help me to solve this
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Single menu item
Set id, icon and Title for each menu item
-->
<item android:id="#+id/menu_bookmark"
android:icon="#drawable/menu_icon"
android:title="Bookmark" />
<item android:id="#+id/menu_save"
android:icon="#drawable/menu_icon1"
android:title="Save" />
<item android:id="#+id/menu_search"
android:icon="#drawable/menu_icon2"
android:title="Search" />
<item android:id="#+id/menu_share"
android:icon="#drawable/menu_icon3"
android:title="Share" />
<item android:id="#+id/menu_delete"
android:icon="#drawable/menu_icon4"
android:title="Delete" />
<item android:id="#+id/menu_preferences"
android:icon="#drawable/menu_icon5"
android:title="Preferences" />
</menu>
AndroidMenusActivity.java
package example.menuexmp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class AndroidMenusActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
// Initiating Menu XML file (menu.xml)
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
/**
* Event Handling for Individual menu item selected
* Identify single menu item by it's id
* */
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_bookmark:
// Single menu item is selected do something
// Ex: launching new activity/screen or show alert message
Toast.makeText(AndroidMenusActivity.this, "Bookmark is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_save:
Toast.makeText(AndroidMenusActivity.this, "Save is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_search:
Toast.makeText(AndroidMenusActivity.this, "Search is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_share:
Toast.makeText(AndroidMenusActivity.this, "Share is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_delete:
Toast.makeText(AndroidMenusActivity.this, "Delete is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_preferences:
Toast.makeText(AndroidMenusActivity.this, "Preferences is Selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Hiii Check this links..
and tell us on which Version are you testing.
You should Check your API versions and Code as per the requirment,
Here
Here
I just added a Menu to my activity. Additionally I want to set some icons to each menu item. Therefore I tried both these methods but I do not see these icons on the device. What am I doing wrong ?
I tried the XML way:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/help"
android:icon="#drawable/help"
android:title="#string/menu_help" />
<item android:id="#+id/settings"
android:icon="#drawable/settings"
android:title="#string/menu_settings" />
<item android:id="#+id/num"
android:icon="#drawable/num_icon"
android:title="#string/menu_num" />
</menu>
Method 2:
In my code:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
//Set icon for the menu button
Drawable num_icon = getResources().getDrawable(R.drawable.num_icon);
menu.getItem(2).setIcon(num_icon);
Drawable settings_icon = getResources().getDrawable(R.drawable.settings);
menu.getItem(1).setIcon(settings_icon);
Drawable help_icon = getResources().getDrawable(R.drawable.help);
menu.getItem(0).setIcon(help_icon);
return true;
Also, could anyone tell me how big should these icon sizes be? Mine is256x256 pix. Thanks
In the android sdk menu icons are of the following sizes:
ldpi 32 x 32 px
mdpi: 36 x 36 px
hdpi: 48 x 48 px
xhdpi: 64 x 64 px
256x256 is too large.
use following code it may help you..
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Single menu item
Set id, icon and Title for each menu item
-->
<item android:id="#+id/menu_bookmark"
android:icon="#drawable/icon_bookmark"
android:title="Bookmark" />
<item android:id="#+id/menu_save"
android:icon="#drawable/icon_save"
android:title="Save" />
<item android:id="#+id/menu_search"
android:icon="#drawable/icon_search"
android:title="Search" />
<item android:id="#+id/menu_share"
android:icon="#drawable/icon_share"
android:title="Share" />
<item android:id="#+id/menu_delete"
android:icon="#drawable/icon_delete"
android:title="Delete" />
<item android:id="#+id/menu_preferences"
android:icon="#drawable/icon_preferences"
android:title="Preferences" />
</menu>
Now open your main Activity class file and put following code
MenusActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class AndroidMenusActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
// Initiating Menu XML file (menu.xml)
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
/**
* Event Handling for Individual menu item selected
* Identify single menu item by it's id
* */
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_bookmark:
// Single menu item is selected do something
// Ex: launching new activity/screen or show alert message
Toast.makeText(AndroidMenusActivity.this, "Bookmark is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_save:
Toast.makeText(AndroidMenusActivity.this, "Save is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_search:
Toast.makeText(AndroidMenusActivity.this, "Search is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_share:
Toast.makeText(AndroidMenusActivity.this, "Share is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_delete:
Toast.makeText(AndroidMenusActivity.this, "Delete is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_preferences:
Toast.makeText(AndroidMenusActivity.this, "Preferences is Selected", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
You just have need to small changes in your manifest file according to your need you can change theme I get the solution for above problem with small change i.e.even using any version of android it will work for (2.3 to latest version 4.4 kitkat also).
<application
android:allowBackup="true"
android:debuggable="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.WithActionBar">
In my application, I manage the options menu to reflect the state my application is in. Three menus are possible.
In the onCreateOptionsMenu function, I inflate the basic entries from an XML file.
In the onPrepareOptionsMenu function, I clear the menu and then inflate the XML file which corresponds to the current state of my application.
And this leads to unexpected results!!!
When I change the menu from state 1 to state 2 and then revert to state 1, the "more" entry is visually replaced by one of the entries of my menu. But it's function is correct : if I click on it, the overflow menu appears!
And then, if I change for state 3, no menu appears when I try to open it. And if I debug the code, I see the menu is correctly filled with the result of the inflation (the size of the menu changes accordingly).
I can add that the code works perfectly with Android 2.3.3. The problem only occurs when I execute it on Android 4 (not tested with Android 3).
Any idea?
As requested, I have simplified my code to let you see the problem.
The code of the application is below :
public class MainActivity extends Activity{
private boolean m2=false,m3=false;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflateur;
inflateur = getMenuInflater();
inflateur.inflate(R.menu.menu_1,menu);
return (true);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuInflater inflateur;
inflateur = getMenuInflater();
menu.clear();
if (m2){
inflateur.inflate(R.menu.menu_2,menu);
} else {
if(m3) {
inflateur.inflate(R.menu.menu_3, menu);
} else {
inflateur.inflate(R.menu.menu_1, menu);
}
}
return (true);
}
#Override
public boolean onOptionsItemSelected(MenuItem entréeMenu) {
switch (entréeMenu.getItemId()) {
case R.menu.m1_4: {
return true;
}
case R.menu.m1_5: {
return true;
}
case R.menu.m1_7: {
return true;
}
case R.menu.m1_8: {
return true;
}
case R.menu.m1_10: {
return true;
}
case R.menu.m1_12: {
return (true);
}
case R.menu.m1_13: {
return (true);
}
case R.menu.m1_1: {
return true;
}
case R.menu.m1_11: {
// change for menu_2
m2=true;
m3=false;
return true;
}
case R.menu.m3_1: {
return true;
}
case R.menu.m3_2: {
return true;
}
case R.menu.m2_1: {
// revert to menu_1
m2=false;
return true;
}
case R.menu.m2_2: {
return true;
}
case R.menu.m1_2: {
// change for menu_3
m2=false;
m3=true;
return true;
}
case R.menu.m1_3: {
return true;
}
case R.menu.m3_3: {
// revert to menu_1
m3=false;
return true;
}
case R.menu.m2_5: {
return true;
}
case R.menu.m2_3: {
return true;
}
case R.menu.m2_4: {
return true;
}
case R.menu.m1_6: {
return (true);
}
case R.menu.Options: {
return (true);
}
case R.menu.m1_14:{
return(true);
}
default: {
return super.onOptionsItemSelected(entréeMenu);
}
}
}
}
Only four menu entries are active : 1.11 changes from menu_1 to menu_2, 2.1 reverts to menu_1; 1.2 changes from menu_1 to menu_3 and 3.3 reverts to menu_1.
The problems encountered so far :
1) click on 1.11 to change for menu_2. Open the menu : only 5 menu items are displayed. Close the menu and re-open it : the 6 menu items are now correctly displayed.
2) click on 2.1 to return to menu 1. In the sixth position, you can see "2.6" instead of "more". If you click on "2.6", the behaviour corresponds to a click on "more".
And now, the XML files for the menus :
menu_1 :
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+menu/m1_1"
android:title="1.1">
</item>
<item android:id="#+menu/m1_2"
android:title="1.2">
</item>
<item android:id="#+menu/m1_3"
android:title="1.3">
</item>
<item android:id="#+menu/m1_4"
android:title="1.4">
</item>
<item android:id="#+menu/m1_5"
android:title="1.5"
android:checkable="true">
</item>
<item android:id="#+menu/m1_6"
android:title="1.6"
android:checkable="true">
</item>
<item android:id="#+menu/m1_7"
android:title="1.7"
android:checkable="true">
</item>
<item android:id="#+menu/m1_8"
android:title="1.8"
android:checkable="true">
</item>
<item android:id="#+menu/m1_9"
android:title="1.9"
android:enabled="false"
android:visible="false"
android:checkable="true">
</item>
<item android:id="#+menu/m1_10"
android:title="1.10">
</item>
<item android:id="#+menu/m1_11"
android:title="1.11">
</item>
<item android:id="#+menu/m1_12"
android:title="1.12">
</item>
<item android:id="#+menu/m1_13"
android:title="1.13">
</item>
<item android:id="#+menu/m1_14"
android:title="1.14">
</item>
<item android:id="#+menu/Options"
android:title="1.15">
</item>
menu_2 :
<item android:id="#+menu/m2_2"
android:title="2.2">
</item>
<item android:id="#+menu/m2_3"
android:title="2.3">
</item>
<item android:id="#+menu/m2_4"
android:title="2.4">
</item>
<item android:id="#+menu/m2_5"
android:title="2.5">
</item>
<item android:id="#+menu/Options"
android:title="2.6">
</item>
</menu>
and menu_3 :
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+menu/m3_1"
android:title="3.1">
</item>
<item android:id="#+menu/m3_2"
android:title="3.2">
</item>
<item android:id="#+menu/m3_3"
android:title="3.3">
</item>
<item android:id="#+menu/m1_4"
android:title="3.4">
</item>
<item android:id="#+menu/Options"
android:title="3.5">
</item>
</menu>
I feel this may be a stupid question but I have no idea what to do.
I have a menu which works correctly. One of the items in the menu: "Search" brings up a submenu with different items like "Restaurant", "Cafe", etc.
Below is the XML code for creating the menu and sub menu:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_entry"
android:title="#string/new_entry"
android:icon="#drawable/add_new">
</item>
<item android:id="#+id/search"
android:title="#string/search"
android:icon="#drawable/search">
<menu>
<item android:id="#+id/five"
android:title="#string/five">
</item>
<item android:id="#+id/ten"
android:title="#string/ten">
</item>
<item android:id="#+id/fifteen"
android:title="#string/fifteen">
</item>
<item android:id="#+id/restaurant"
android:title="#string/restaurant">
</item>
<item android:id="#+id/cafe"
android:title="#string/cafe">
</item>
<item android:id="#+id/sandwich"
android:title="#string/sandwich">
</item>
</menu>
</item>
<item android:id="#+id/info"
android:title="#string/info_short"
android:icon="#drawable/info">
</item>
Then in my Activity Class I have:
// Initialise Menu.
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_services, menu);
return true;
}
and:
// Create cases for each menu selection.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.new_entry:
createFood();
return true;
case R.id.search:
return true;
case R.id.cafe:
Log.d(TAG, "In Cafe SubMenu");
Toast.makeText(getApplicationContext(), TAG, Toast.LENGTH_SHORT);
return true;
case R.id.info:
info();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Each case in "onOptionsItemSelected" does what it is supposed to do, except "case R.id.cafe:" which is the submenu. Here it should post to the LogCat and a Toast for testing but does neither.
What am I missing?
Thanks
I have tried your code and it is working perfectly. Only the Toast is not displaying when I select 'cafe'. That is because, you did not call show() in your code. Your code is
Toast.makeText(getApplicationContext(), TAG, Toast.LENGTH_SHORT);
to show the toast on run time it should call as follows
Toast.makeText(getApplicationContext(), TAG, Toast.LENGTH_SHORT).show();
:)
I think you should remove following lines:
case R.id.search:
return true;