Create an option menu - android

I'm able to create an option menu in Android through XML file, but I would like to add the second menu programmatically just before the menu is added from XML.
In example: the code below adds a menu item search.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="#drawable/search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
</menu>
If I add a new menu item programatically, like
menu.add(0,Menu.FIRST, Menu.NONE,"Gift box").setIcon(R.drawable.jingle_bell).
setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
It is appearing after the Search menu item in Actionbar, but I need it before the search menu item.
What changes do I need to make?

Try this :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.yourmenu, menu);
menu.add(0,Menu.FIRST, Menu.NONE,"Gift box").setIcon(R.drawable.ic_refresh).
setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
Update:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="#drawable/search"
android:orderInCategory="200"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
</menu>

Just set order for your item as,
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.yourmenu, menu);
menu.add(Menu.NONE, Menu.FIRST, 1,"Gift box").setIcon(R.drawable.ic_refresh).
setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
Third parameter is order for above menu item. Now in your menu.xml set order for your item as,
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="#drawable/search"
android:orderInCategory="2"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
</menu>

Try below example
in 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_guide"
android:title="title"
android:icon="#drawable/ic_title"
app:showAsAction="always"
/>
then copy below code to your activity :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main_actions, menu);
return true;
}

Related

How can i get android toolbar menu group

In some pages of stackoverflow said that you can get menu goup with menu.findItem(R.id.groupId) but always return null and i want to know how can i get menu group
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:id="#+id/fdp_m_group_normal">
<item android:id="#+id/fdp_m_today"
app:actionLayout="#layout/menu_today"
app:showAsAction="always|withText"
android:orderInCategory="100"
android:title="today"/>
<item android:id="#+id/fdp_m_tomorrow"
app:actionLayout="#layout/menu_tomorrow"
app:showAsAction="always|withText"
android:orderInCategory="101"
android:title="tomorrow"/>
<item android:id="#+id/fdp_m_after"
app:actionLayout="#layout/menu_after"
app:showAsAction="always|withText"
android:orderInCategory="102"
android:title="after"/>
</group>
<group android:id="#+id/fdp_m_group_range">
<item android:id="#+id/fdp_m_plus1"
app:actionLayout="#layout/menu_plus1"
app:showAsAction="always|withText"
android:orderInCategory="103"
android:title="+1"/>
<item android:id="#+id/fdp_m_plus2"
app:actionLayout="#layout/menu_plus2"
app:showAsAction="always|withText"
android:orderInCategory="104"
android:title="+2"/>
<item android:id="#+id/fdp_m_plus3"
app:actionLayout="#layout/menu_plus3"
app:showAsAction="always|withText"
android:orderInCategory="105"
android:title="+3"/>
</group>
</menu>
and my java code for accessing menu
#Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fdp_menu, menu);
// Below line always return null
MenuItem mGroupOne = menu.findItem(R.id.fdp_m_group_normal);
super.onCreateOptionsMenu(menu, inflater);
}
Getting group using findItem does return null. I am assuming you only need it for visibility. Try using menu.setGroupVisible(R.id,false);
menu.findItem will return null if there is no item in 'menu' with the id.
So first of all check that 'menu' is the right menu to be looking in, and then check in menu.xml that you have declared an item with the correct id.

How to show menu item in action bar?

I want the menu items to show on action bar. There is a lot of space on the ActionBar, but still they don't show up. Every item shown as three dots, why?
.java file
public class GalleryActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_layout);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
}
.xml file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:MissionAndroid="http://schemas.android.com/tools">
<item
android:id="#+id/facebookId"
android:title="facebook"
android:icon="#drawable/facebook"
MissionAndroid:showAsAction="always"/>
<item
android:id="#+id/shareId"
android:title="share"
android:icon="#drawable/share"
MissionAndroid:showAsAction="always"/>
<item
android:id="#+id/delete"
android:title="delete"
android:icon="#drawable/delete"
MissionAndroid:showAsAction="always"/>
<item
android:id="#+id/searchId"
android:title="search"
android:icon="#drawable/search"
MissionAndroid:showAsAction="ifRoom"/>
</menu>
Currently, you're using "http://schemas.android.com/tools" which does not offer the functionality you're looking for and your showAsAction modifiers are being ignored.
Try updating your main_menu.xml with the following:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:MissionAndroid="http://schemas.android.com/apk/res-auto">
...
</menu>
Better yet, to follow convention, replace MissionAndroid in this file with app:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/facebookId"
android:title="facebook"
android:icon="#drawable/facebook"
app:showAsAction="always"/>
...
</menu>

Cross xml reference is not working

I created menu.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_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
but, in my activity xml the menu doesn't appear!
So What can I do?
Thanks!
You need to override the onCreateOptionsMenu() method in your activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu); // Specify your menu 'id' here
return true;
}

Menu icon is not displaying in action bar

I need to inflate custom menu in Fragment.
I have only one menu item.But the icon is not displaying.
Can someone tell what is wrong with my code
My menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" >
<item
android:id="#+id/search"
android:icon="#android:drawable/ic_search_category_default"
app:showAsAction="always"
android:title="Search"/></menu>
And I set in onCreateView()
setHasOptionsMenu(true);
getActivity().invalidateOptionsMenu();
And inflating the menu
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.menu, menu);
}
Resulting screen attached below. I need to have the search icon instead of the menu overflow icon.
I know I'm a little late for the party, but hope I can help someone else. Today I was facing this SAME problem.
I fixed using android:showAsAction="always" instead of app:showAsAction="always"
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/bluetooth_status_off"
android:orderInCategory="0"
android:icon="#drawable/bluetooth_red"
android:title="#string/app_name"
android:showAsAction="always" />
</menu>
on the showAsAction is underlined with red (warning), but works fine.
I Think You should use
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" >
<item
android:id="#+id/search"
android:icon="#drawable/ic_search_category_default"
app:showAsAction="always"
android:title="Search"/></menu>
and
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mymenu, menu);
return true;
}
Sorry to late but put this code in your onCreate()
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
You have to add the following property app:showAsAction="ifRoom"
Make sure, you've added a menu item without an icon.
Example:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/next_btn"
android:checked="true"
android:icon="#android:drawable/ic_input_add"
android:title="Item"
app:showAsAction="ifRoom" />
<item
android:id="#+id/action_settings"
android:title="Settings" />
</menu>
Thanks to action_settings, it'll automatically add an overflow icon.
make a menu with the code below...
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item
android:id="#+id/action_search"
android:title="Search"
android:icon="#android:drawable/ic_menu_search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
</menu>
and set it as your menu..
This is default behaviour of the Overflow menu from the ActionBar.
If you want to show the Icons in the overflow menu, override the onMenuOpened method in your activity with this code snippit:
#Override
public boolean onMenuOpened(int featureId, Menu menu) {
if(featureId == Window.FEATURE_ACTION_BAR && menu != null){
if(menu.getClass().getSimpleName().equals("MenuBuilder")){
try{
Method m = menu.getClass().getDeclaredMethod(
"setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
}
catch(NoSuchMethodException e){
Log.e("MyActivity", "onMenuOpened", e);
}
catch(Exception e){
throw new RuntimeException(e);
}
}
}
return super.onMenuOpened(featureId, menu);
}
If you're running your code on Android 3.0+, the icons in the menu are not shown by design.
This is a design decision by Google.
You can read more about this on Android developers blog.
Copy your menu xml code.
Delete your menu file
Now create a new menu file in res->menu directory
And name it with different name
Now Run

Action Bar Items (Action Items) are not showing in android.

I have two menu items in menu/contacts_menu.xml as :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >
<item
android:id="#+id/pm_action_search"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_action_search"
android:orderInCategory="1"
android:showAsAction="ifRoom|collapseActionView"
android:title="#string/action_search"/>
<item
android:id="#+id/show_online"
android:icon="#drawable/online_icon"
android:orderInCategory="2"
android:showAsAction="ifRoom"
android:title="Show Online"/>
</menu>
Where , in my Fragment :
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.contacts_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.show_online) {
// DO SOMETHING
return true;
}
return super.onOptionsItemSelected(item);
}
I have done the same procedure in my others apps, and action items are showing. But in my current app, they are not showing. only if i press the menu button, action items are showing only with text. i want to show the icons on my action bar.
I guess you are using appcompat library? Try this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/pm_action_search"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_action_search"
android:orderInCategory="1"
android:showAsAction="always|collapseActionView"
appcompat:showAsAction="always|collapseActionView"
android:title="#string/action_search" />
<item
android:id="#+id/show_online"
android:icon="#drawable/online_icon"
android:orderInCategory="2"
android:showAsAction="always"
appcompat:showAsAction="always"
android:title="Show Online" />
</menu>
This should definitely work, but for simplicity you can also try this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/pm_action_search"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_action_search"
android:orderInCategory="1"
appcompat:showAsAction="always|collapseActionView"
android:title="#string/action_search" />
<item
android:id="#+id/show_online"
android:icon="#drawable/online_icon"
android:orderInCategory="2"
appcompat:showAsAction="always"
android:title="Show Online" />
</menu>

Categories

Resources