Menu option in android - android

After referring a lot of tutorials I came to know that instead of Menu they have ActionBar for > API 10. But i am using API 7 sdk for my testing, I have used Menus to show text with drawable images. But only the text is coming and the drawable icon image is not showing in the menu option. Please help me to solve this.
My 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/savedstory"
android:background="#000000"
android:minHeight="20dp"
android:title="Saved Stories"/>
<item
android:id="#+id/setting"
android:background="#000000"
android:minHeight="20dp"
android:title="Settings"/>
<item
android:id="#+id/Bookmark"
android:background="#000000"
android:minHeight="20dp"
android:title="Bookmark This"/>
<item
android:id="#+id/share"
android:background="#000000"
android:minHeight="20dp"
android:title="Share This"/>
<item
android:id="#+id/save"
android:background="#000000"
android:minHeight="20dp"
android:title="Save This"/>
<item
android:id="#+id/small"
android:icon="#drawable/font3"
android:minHeight="20dp">
This icon is not showing.
/>
<item
android:id="#+id/medium"
android:background="#ffffff"
android:minHeight="20dp"
android:title="Medium font"/>
<item
android:id="#+id/big"
android:background="#000000"
android:minHeight="20dp"
android:title="Big font"/>
</item>
</menu>
My inflating code:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.newsdescriptionmenu, menu);
return true;
}

If you refer to Menu documentation"
Options menus: The icon menus do not support item check marks and only
show the item's condensed title. The expanded menus (only available
if six or more menu items are visible, reached via the 'More' item in
the icon menu) do not show item icons, and item check marks are
discouraged.
Since I cannot see how you inflate (what options, etc) your menus I can only assume that you don't see this item's icon as it is a sixth item and hits the expanded menu after 'More'.

Please check the following code snippet.
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/Menu1"
android:orderInCategory="1"
android:title="Menu 1"/>
<item
android:id="#+id/Menu2"
android:orderInCategory="2"
android:title="Menu 2"/>
<item
android:id="#+id/Menu3"
android:orderInCategory="3"
android:title="Menu 3"/>
<item
android:id="#+id/submenu"
android:orderInCategory="4"
android:title="Sub menu">
<menu>
<item
android:id="#+id/submenu1"
android:title="Sub menu 1"/>
<item
android:id="#+id/submenu2"
android:title="Sub menu 2"/>
</menu>
</item>
</menu>
Add these lines in your Activity Class
public class MenuActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(getApplication()).inflate(R.menu.menu, menu);
return(super.onPrepareOptionsMenu(menu));
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.Menu1:
Toast.makeText(this, "Menu 1", Toast.LENGTH_SHORT).show();
break;
case R.id.Menu2:
Toast.makeText(this, "Menu 2", Toast.LENGTH_SHORT).show();
break;
case R.id.Menu3:
Toast.makeText(this, "Menu 3", Toast.LENGTH_SHORT).show();
break;
case R.id.submenu:
Toast.makeText(this, "Sub menu", Toast.LENGTH_SHORT).show();
break;
}
return(super.onOptionsItemSelected(item));
}
}

Related

Display menu items on ActionBar

I want to display my menu items on ActionBar with android
I found some samples but It was not what I wanted
Menu should be similar picture below(Overflow Icon or text)
My menu.xml is :
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_refresh"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="#drawable/icon"
android:visible="true"
android:title="Refresh"/>
<item
android:id="#+id/action_settings"
android:title="Settings">
<item
android:id="#+id/mapMenu"
android:title="map menu" />
<item
android:id="#+id/favMenu"
android:title="favorite" />
<item
android:id="#+id/listMenu"
android:title="List Menu" />
</item>
and my android code :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(getApplicationContext(), item.getTitle() + " selected", Toast.LENGTH_SHORT).show();
switch (item.getItemId()) {
case R.id.mapMenu:
// do something
break;
case R.id.favMenu:
// do something
break;
case R.id.listMenu:
// do something
break;
}
return true;
}
Once write following in all item tags
android:showAsAction="never"
then all items will display in overflow.
Ex:
<item
android:id="#+id/mapMenu"
android:showAsAction="never"
android:title="map menu" />
Hope this will helps you.
If you want to show the three dots, irrespective of device menu button, then you can call below code (which is based on reflection) in your application class' onCreate method-
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}
Caution: This is an awful hack that breaks consistency with the rest
of the apps on the platform
Thanks all
but I added android:showAsAction="ifRoom|withText" and result is :
I also tested android:showAsAction="ifRoom|withText" and menu items hid
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/main_menu"
android:showAsAction="always"
android:icon="#drawable/icon"
android:visible="true">
<item
android:id="#+id/action_refresh"
android:showAsAction="never"
android:title="Refresh"/>
<item
android:id="#+id/action_settings"
android:title="Settings"
android:showAsAction="never" />
<item
android:id="#+id/mapMenu"
android:title="map menu"
android:showAsAction="never" />
<item
android:id="#+id/favMenu"
android:title="favorite"
android:showAsAction="never" />
<item
android:id="#+id/listMenu"
android:title="List Menu"
android:showAsAction="never" />
</item>
</menu>
android:showAsAction: When and how this item should appear as an action item in the Action Bar. A menu item can appear as an action item only when the activity includes an ActionBar (introduced in API Level 11).
Try the given code. First you create root item keep its android:showAsAction:Always then inside that root item create other items with android:showAsAction:never.

how to show menu item under overflow icon in android api 8+

How I can force menu item to show under overflow icon. I am providing app support from api level 8 and above. for now it is showing as actionview .
My menuLayout is as follows.
//main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_menu_setting"
android:title="#string/menuItemSetting"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/action_signout"
android:title="#string/menuItemLogout"
app:showAsAction="ifRoom"/>
</menu>
in Java class
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
I have tried with other options of showAsAction but none of them worked. Please suggest me how I can show above two menu itme under overflow icon(when i click on over these two will appearer as actionlist.)
layout for custom action bar
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="#drawable/header"
android:orientation="vertical" >
<TextView
android:id="#+id/tvActionBarTitle"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:gravity="center"
android:textColor="#FFFFFF"
android:textSize="30sp" />
<Button.../>
<!-- your code for button -->
</LinearLayout>
You can call below function in onCreate and define button in this function as I have defined appName text view and its OnClick event
private void createCutomActionBarTitle()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH)
{
this.getActionBar().setHomeButtonEnabled(true);
}
this.getActionBar().setDisplayShowCustomEnabled(true);
this.getActionBar().setDisplayShowTitleEnabled(false);
this.getActionBar().setBackgroundDrawable(drawable);
this.getActionBar().setIcon(logo);
LayoutInflater inflator = LayoutInflater.from(this);
View v = inflator.inflate(R.layout.custom_action_bar, null);
((TextView) v.findViewById(R.id.tvActionBarTitle)).setText(Global.ACTIVITY_NAME);
this.getActionBar().setCustomView(v);
}
Hope this will be useful for you :)
But clicking physical button of device will not trigger this button you have to look for it, as I don't have any idea about it
Could you post java code ? Do you have code below in your activity ?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.menu, menu);;
return super.onCreateOptionsMenu(menu);
}
Edit :
I alway use code like this(when i click action_settings it will show action list contains setting and bar, i think just replace android:title="#string/action_settings" with android:icon)
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="always"
android:title="#string/action_settings">
<menu>
<item
android:enabled="true"
android:visible="true"
android:title="setting"
android:id="#+id/setting">
</item>
<item
android:enabled="true"
android:visible="true"
android:title="B"
android:id="#+id/bar">
</item>
</menu>
</item>
<item
android:id="#+id/action_settings1"
android:orderInCategory="100"
android:showAsAction="always"
android:title="#string/action_settings"/>
</menu>
Hope it helps

Android Menu does not inflate

I am pulling my hairs out in trying to get menu to infate on my main activity but to no avail.
I am using Android Studio and the design view showed that the XML menu items are rightfully defined.
However, when I run the codes either in the phone(running Android 4.4.2) or emulator, the menu items are not showing up. Nothing happens when I press on the menu.
Any enlightenment will be most welcome.
Menu XML - my.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/item1" android:title="Option1"></item>
<item android:id="#+id/item2" android:title="Option2"></item>
<item android:id="#+id/item3" android:title="Option3"></item>
<item android:id="#+id/item4" android:title="Option4"></item>
<item android:id="#+id/item5" android:title="Option5"></item>
<item android:id="#+id/item6" android:title="Option6"></item>
</menu>
Activity Class - myActivity.java
package com.example.mymenu;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.util.Log;
import android.widget.Toast;
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(this, " " +
"Option1", Toast.LENGTH_SHORT).show();
return true;
case R.id.item2:
Toast.makeText(this, "Option2", Toast.LENGTH_SHORT).show();
return true;
case R.id.item3:
Toast.makeText(this, "Option3", Toast.LENGTH_SHORT).show();
return true;
case R.id.item4:
Toast.makeText(this, "Option4", Toast.LENGTH_SHORT).show();
return true;
case R.id.item5:
Toast.makeText(this, "Option5", Toast.LENGTH_SHORT).show();
return true;
case R.id.item6:
Toast.makeText(this, "Option6", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Layout File
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MyActivity">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
I have solved the problem.
My phone has a menu button, and pressing that button will display the menu list.
I tried with a newer phone that does not have the menu button, and the menu shows up within the action bar.
Regards
I think the problem is you must have created my.xml in layout folder .Create my.xml in menu folder.
Cross check if the menu.xml is in menu folder and not any other res folder like layout.
Trying to inflate it in action bar?
If yes, then try using the following code in the menu.xml
android:showAsAction="always"
Refer : http://developer.android.com/guide/topics/ui/menus.html
Hi try this if you want to display it in Actionbar.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:icon="#drawable/action_overflow"
app:showAsAction="always"/>
<menu>
<item android:id="#+id/item1" android:title="Option1"></item>
<item android:id="#+id/item2" android:title="Option2"></item>
<item android:id="#+id/item3" android:title="Option3"></item>
<item android:id="#+id/item4" android:title="Option4"></item>
<item android:id="#+id/item5" android:title="Option5"></item>
<item android:id="#+id/item6" android:title="Option6"></item>
</menu>
</menu>

how to add new item to setting menu at Android?

i need to add facebook like at setting menu as below image .so how to add new item to setting menu I tried to solve this issue only i had found the setting menu at res > values >string.xml > settings menu .
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<menu>
<item
android:id="#+id/action_settings"
android:orderInCategory="1"
android:showAsAction="never"
android:title="Settings"/>
<item
android:id="#+id/action_about"
android:orderInCategory="2"
android:showAsAction="never"
android:title="About"/>
<item
android:id="#+id/action_exit"
android:orderInCategory="3"
android:showAsAction="never"
android:title="Exit"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/web_engine"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</WebView>
Open '/res/menu/menu.xml'
Add this code in it:
<item
android:id="#+id/action_about"
android:orderInCategory="2"
android:showAsAction="never"
android:title="About"/>
<item
android:id="#+id/action_exit"
android:orderInCategory="3"
android:showAsAction="never"
android:title="Exit"/>
Open '/src/(packagename)/(acitivityname).java'
Add this code there
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_about:
// About option clicked.
return true;
case R.id.action_exit:
// Exit option clicked.
return true;
case R.id.action_settings:
// Settings option clicked.
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Android adding a submenu to a menuItem, where is addSubMenu()?

I want to add a submenu inside my OptionsMenu to a menuItem, programatically according to my parameters. I've checked "MenuItem" in android sdk and there is no addSubMenu() method!, although you can find "hasSubMenu()" and "getSubMenu".
Was thinking on doing this in onCreateOptionsMenu:
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem mi = menu.getItem(MYITEMID); // << this is defined in my XML optionsMenu
SubMenu subm = mi.addSubMenu(0,1,0,"Map 1"); // no addSubMenu() method!!!???
....
How do I create a submenu inside a menuitem in code?
Sometimes Android weirdness is really amazing (and amusing..). I solved it this way:
a) Define in XML a submenu placeholder like this:
<item android:visible="true" android:id="#+id/m_area"
android:titleCondensed="Areas"
android:title="Areas"
android:icon="#drawable/restaur"
android:enabled="true">
<menu>
<item android:id="#+id/item1" android:title="Placeholder"></item>
</menu>
</item>
b) Get sub menu item in OnCreateOptionsMenu, clear it and add my submenu items, like this:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mapoptions, menu);
int idx=0;
SubMenu subm = menu.getItem(MYITEM_INDEX).getSubMenu(); // get my MenuItem with placeholder submenu
subm.clear(); // delete place holder
while(true)
{
anarea = m_areas.GetArea(idx); // get a new area, return null if no more areas
if(anarea == null)
break;
subm.add(0, SUBAREASID+idx, idx, anarea.GetName()); // id is idx+ my constant
++idx;
}
}
I know this is an old question, but I just came across this problem myself.
The most straightforward way of doing this, seems to be to simply specify the item itself as a submenu, then add to this item.
E.g.:
menu.add(groupId, MENU_VIEW, Menu.NONE, getText(R.string.menu_view));
menu.add(groupId, MENU_EDIT, Menu.NONE, getText(R.string.menu_edit));
SubMenu sub=menu.addSubMenu(groupId, MENU_SORT, Menu.NONE, getText(R.string.menu_sort));
sub.add(groupId, MENU_SORT_BY_NAME, Menu.NONE, getText(R.string.menu_sort_by_name));
sub.add(groupId, MENU_SORT_BY_ADDRESS, Menu.NONE, getText(R.string.menu_sort_by_address));
:
:
Here's a complete answer which builds on the idea of using a placeholder but uses mostly xml to add the submenu.
If you have a menu like so called main_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="My Menu"
android:id="#+id/my_menu_item">
<!-- A empty SubMenu -->
<menu></menu>
</item>
</menu>
Create another menu sub_menu.xml which will be used in my_menu_item:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="SubMenu One"
android:id="#+id/submenu_one" />
<item android:title="SubMenu Two"
android:id="#+id/submenu_two" />
<item android:title="SubMenu Three"
android:id="#+id/submenu_three" />
</menu>
In your onCreateOptionsMenu:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate your main_menu into the menu
getMenuInflater().inflate(R.menu.main_menu, menu);
// Find the menuItem to add your SubMenu
MenuItem myMenuItem = menu.findItem(R.id.my_menu_item);
// Inflating the sub_menu menu this way, will add its menu items
// to the empty SubMenu you created in the xml
getMenuInflater().inflate(R.menu.sub_menu, myMenuItem.getSubMenu());
}
This solution is nice since the inflater handles most of the work.
The best way to do this is in your xml menu file. You can do this by creating a new menu object inside of an item:
<menu>
<item>
...
<menu>
...
</menu>
...
</item>
</menu>
To provide a comprehensive example of Phil's answer, here is my complete, working XML for a menu with two choices, each of which is a menu with three choices. I intend to add a third menu to the top level ...
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:HTMLCode="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/Examine"
android:title="#string/Examine"
HTMLCode:showAsAction="always">
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:HTMLCode="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/load"
android:title="#string/load"
HTMLCode:showAsAction="ifRoom|withText" />
<item android:id="#+id/findfirst"
android:title="#string/findfirst"
HTMLCode:showAsAction="ifRoom|withText" />
<item android:id="#+id/findnext"
android:title="#string/FindNext"
HTMLCode:showAsAction="ifRoom|withText" />
</menu>
</item>
<item android:id="#+id/Redirect"
android:title="#string/Redirect"
HTMLCode:showAsAction="ifRoom|withText">
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:HTMLCode="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/getRedirect"
android:title="#string/getRedirect"
HTMLCode:showAsAction="ifRoom|withText" />
<item android:id="#+id/toggleRedirect"
android:title="#string/toggleRedirect"
HTMLCode:showAsAction="ifRoom|withText" />
<item android:id="#+id/copyRedirect"
android:title="#string/copyRedirect"
HTMLCode:showAsAction="ifRoom|withText" />
</menu>
</item>
</menu>
You should consider use a ActionProvider instead.
public class MyActionProvider extends ActionProvider {
private Context mContext;
public MyActionProvider(Context context) {
super(context);
mContext = context;
}
#Override
public View onCreateActionView() {
//LayoutInflater layoutInflater = LayoutInflater.from(mContext);
return null;
}
#Override
public void onPrepareSubMenu(SubMenu subMenu) {
super.onPrepareSubMenu(subMenu);
subMenu.clear();
subMenu.add("menu 1");
subMenu.add("menu 2");
subMenu.add("menu 3");
}
#Override
public boolean hasSubMenu() {
return true;
}
#Override
public boolean onPerformDefaultAction() {
return super.onPerformDefaultAction();
}
}
I would just create the submenu in xml file, and in run time get the submenu from menu object, (using findItem(id) method) and use submenu.setVisible(boolean) to add/remove it on run time.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu1" android:alphabeticShortcut="a"
android:title="Menu No. 1" android:orderInCategory="1" />
<item android:id="#+id/menu2" android:alphabeticShortcut="b"
android:title="Menu No. 2" android:orderInCategory="2">
<menu >
<group android:id="#+id/group2" android:checkableBehavior="single">
<item android:id="#+id/submenu1" android:title="SubMenu No. 1" />
<item android:id="#+id/submenu2" android:title="SubMenu No. 2" />
</group>
</menu>
</item>

Categories

Resources