How I can add two menu in ActionBar Sherlock? - android

I used this guide to create the main application menu.
Next, I added a button (three dots on the left) and the button I want to add another menu. It is important that the menu was different for each fragment
Code of button:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.actlist, menu);
return true;
}
and xml file for this:
<menu xmlns:android=
"http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/act"
android:icon="#drawable/ic_action_overflow"
android:showAsAction="always"
android:title="title add"
android:titleCondensed="add">
</item>
If I try to add a menu, it replaces the main menu.
Thanks!

Items that don't fit in the actionbar are automatically added to the overflow, there is no need to create another menu. If you want to force the itmes to be in the overflow you can use android:showAsAction="never"

Related

How to add only one item from menu.xml to overflow menu in android?

I have menu xml file in menu->main.xml in which two item are available. I have two activities and i want to add this two item in overflow menu in first activity but in the second activity i want to add only one item in overflow menu.
menu->main.xml
<?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/action_refresh"
android:title="#string/action_refresh"
app:showAsAction="never"/>
<item
android:id="#+id/action_setting"
android:title="#string/action_setting"
app:showAsAction="never"/>
</menu>
So how to do that?
you can do it in two ways:
Have separate menu XMLs for both activities. one with both items and other with only one that you require.
remove the unneeded menu item in code inside 2nd activity, by overriding onCreateOptionsMenu e.g:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main,menu);
menu.removeItem(R.id.action_setting);
return super.onCreateOptionsMenu(menu);
}
you could do it programmatically by hiding it...For example
MenuItem refreshIcon = menu.findItem(R.id. action_refresh);
refreshIcon.setVisible(false);
Get a reference to your menu item in onCreateOptionsMenu(menu) and update show as action as below in your second activity.
MenuItem menuitem = menu.finditem(your_id);
menuitem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
You can use the onCreateOptionsMenu that you'll have to override. You could try something like that, depending on what you need to show here:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.getItem(0).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}

Menu item always goes in the overflow menu in Android

I have a menu icon for my ActionBar, but it always goes into the overflow menu no matter what I do. I want it to show up as an icon and not go into the overflow menu.
Here is my menu_daily_selfie.xml:
<?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/camera_button"
android:icon="#android:drawable/ic_menu_camera"
android:title="Camera"
app:showAsAction="always"/>
</menu>
I'm using the appcompat library. What gives? Seems like this should work.
Here is my onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_daily_selfie, menu);
return true;
}
My main activity extends ListActivity since it uses a ListView
Then you are not using appcompat-v7 properly. Either:
Switch to inheriting from AppCompatActivity, and manage your own ListView, or
Stop using appcompat-v7, and switch your menu resource to use android:showAsAction instead of app:showAsAction

about android toolbar, how to hide defualt setting menu in toolbar

about android Toolbar, how to hide default setting menu in toolbar
in the this activity i hava set a single icon menu,like this –
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:title=""
android:id="#+id/menu_add"
android:icon="#drawable/actionbar_add_icon"
android:showAsAction="ifRoom">
</item>
</menu>
it is menu/main_home.xml
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_home, menu);
return super.onCreateOptionsMenu(menu);
}
but it show a three dots menu. i want know why
and how to show sub menu icon image ,it is normal in the Actionbar, but hide in the Toolbar
If you want to empty the contents of the menu, go to the res/menu folder and delete the item tags from the menu_main.xml file. You will also need to delete references to the items in onOptionsItemSelected(MenuItem item). The menu dots will disappear if there are no items.
If you want to completely remove the three dots menu, then go to the onCreateOptions(...) method in your code and delete it, or simply remove the getMenuInflater().inflate(...); portion of it. You can safely remove the onOptionsItemSelected(MenuItem item) method as well because it will have no purpose.
The simplest way to make the menu disappear is to have the onCreateOptions(...) method return false.
this question is solved use app:showAsAction ="ifRoom" not android:showAsAction ="ifRoom"
<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:title=""
android:id="#+id/menu_button"
app:actionLayout="#layout/menu_single_button"
app:showAsAction = "always"
/></menu>

How to get menu options in actionbar aligned with fragments?

On a tablet in landscape, the GMail app has the following:
The magnifying glass menu item appears to be aligned to the right side of the list fragment.
How can I achieve this in my own app?
Step I: Create a xml in the res/menu folder.
Step II: Add the following code snippet in the menu_main.xml or the pre-provided menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:title="#string/action_search"
android:icon="#drawable/ic_action_search_hdpi"
yourapp:showAsAction="always"
android:enabled="true"
yourapp:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
`
Step III: Inflate the menu.xml in the onCreateOptionsMenu(Menu menu)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.menu_main, menu);
return true;
}
Since Search is also a menu option, android already provides the widget to use it seamlessly.You can also handle a click listener to integrate the search mechanism into the app with the help of API's.
I hope it helps.

Menu overflow button on ActionBar/ActionBarSherlock

This app was designed before the deprecation of the hardware menu button, so now it automatically shows a menu overflow button at the bottom of our app. We'd rather it wedge itself to the right of our 4 tab bar items when needed.
If that's not doable, we'd at least like to be able to center the lonely menu overflow button like it appears on the first pic here -> http://www.droid-life.com/2012/05/30/dear-developers-can-we-quit-with-the-menu-button-already-and-adopt-an-action-overflow/
We are using ActionBarSherlock.
You must add it mannually with setAddOptionMenu(true);
and then create it in xml for example:
<item
android:id="#+id/video"
android:title="#string/video"
android:icon="#drawable/video"
android:showAsAction="ifRoom|withText"
/>
<item
android:id="#+id/email"
android:title="#string/email"
android:icon="#drawable/email"
android:showAsAction="ifRoom|withText"
/>
Then inflate it
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.items, menu);
return super.onCreateOptionsMenu(menu);
}
if you want to split the actionBar in the bottom of the screen you can use
android:uiOptions=”splitActionBarWhenNarrow”
in your activity in the manifest
hope it helps

Categories

Resources