I have an action bar in my activity with 3 items - Add item, Options item and Overflow item.
Add item is set to always be visible in the action bar, so is the overflow item.
My problem is with the Options item - I want it to be shown at the overflow menu, but whenever I click at the overflow item(the icon with the 3 dots), it simply does nothin. However, if I click built-in menu button on the device, it shows me the overflow menu with my Options item.
So my question is - why when I click the overflow icon, nothing happens but when I click the built-in menu button on the device, it does show me the overflow menu?
Here is my XML menu code:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_add"
android:icon="#drawable/ic_action_new"
android:showAsAction="ifRoom"
android:title="#string/action_add_ringtone"/>
<item
android:id="#+id/action_set_options"
android:showAsAction="never"
android:title="Options" />
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_action_overflow"
android:orderInCategory="100"
android:showAsAction="always"
android:title="#string/action_settings"/>
</menu>
Here is my implementation for onOptionsItemSelected :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_settings:
return super.onOptionsItemSelected(item);
case R.id.action_add:
pickRingtone(1000);
return true;
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
You will need to override the onCreateOptionsMenu method (Ctrl+O in Android Studio).
#Override
public void onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your_menu_name, menu);
}
For a fragment the MenuInflater will already be a parameter, and you will need to call setHasOptionsMenu(true); somewhere such as at the end of onCreate as well.
Related
well the problem is that my icon of my option add item on the action bar does not appear(it has space for show the icon but it does not). my option just appear on the dropdown menu (the 3 points XD) thanks you. here is my code xml then my code java:
<?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/menuAddItem"
android:title="Add Item"
android:icon="#android:drawable/ic_menu_add"
app:showAsAction="ifRoom"
/>
</menu>
java:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.clear();
MenuInflater infladorMenu = new MenuInflater(this);
infladorMenu.inflate(R.menu.menu,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menuAddItem:
this.listaDatos.add("Dato AƱadido");
this.adaptador.notifyDataSetChanged();
break;
}
return super.onOptionsItemSelected(item);
}
As you can see the icon does not appear but the actionbar has space
As you can see the icon does not appear but the actionbar has space, my option is just showed in the dropdown menu. thank you for your help.
I'm practicing follow this guide http://developer.android.com/training/basics/actionbar/adding-buttons.html
but it doesn't display icon "search" on action bar, it just have "Search" string after I clicked on "three dots". How to remove three dots and showing search icon on Action Bar?
My app like this but there is no search icon, but there is a "Search" string in "three dots"
add this to your menu item:
android:icon="#drawable/my_icon"
app:showAsAction="ifRoom"
What you have to do is setHasOptionsMenu method in true and override onCreateOptionsMenu and onOptionsItemSelected if you are using fragments.
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.events_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
openSearchView();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
This is the menu xml file that displays a search option on the ActionBar:
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/search"
app:showAsAction="always" />
You can remove the 3-dots menu by just adding below attribute android:showAsAction="never" to it:
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
And also use app:showAsAction="always" in all other menu items.
Hope this helps!
I have seen this question asked many times, however, none of those solutions have helped me. My problem is that I have an ActionBar menu and I want its items to always be displayed on the ActionBar instead of the drop down menu.
I have tried actions suchs as "ifRoom", "always", etc. and they still show only with text on the drop down menu.
Menu: devotional_fragment_actions.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_previous"
android:icon="#drawable/ic_action_previous_item"
android:title="#string/previous_action"
app:showAsAction="always"/>
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_go_to_today"
android:title="#string/date_action"
app:showAsAction="always"/>
<item android:id="#+id/action_next"
android:icon="#drawable/ic_action_next_item"
android:title="#string/next_action"
app:showAsAction="always"/>
</menu>
On my fragment I have:
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// action bar
inflater.inflate(R.menu.devotional_fragment_actions, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle item selection of action bar
switch (item.getItemId()) {
case R.id.action_search:
showDatePickerDialog();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The menu works fine but is not displaying as I want it. Thanks
In each item use android namespace instead of app. You can even remove this namespace declaration xmlns:app="http://schemas.android.com/apk/res-auto".
Try set it like in code below:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_previous"
android:icon="#drawable/ic_action_previous_item"
android:title="#string/previous_action"
android:showAsAction="always"/>
</menu>
I want to use action bar in my application. So far I was able to add the action bar from the support library. Now I want to add items to my action bar. I want the icons of the items to be displayed in my action bar, so I did the following:
first I created menu.xml file
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_locate"
android:icon="#drawable/ic_action_location_found"
android:title="#string/locate"
android:showAsAction="always" />
<!-- Settings, should always be in the overflow -->
and I added these functions to my mainActivity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_locate:
Toast.makeText(this,"locate is selected",Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
When I run my application I only see the title of the action bar, the locate icon is not there. When I touch the options button on my phone a list with only locate in it appears. What I need is to have locate icon appearing on the right corner of the action bar. Can anybody please tell me what I'm doing wrong and why its not appearing on the right corner of the action bar??
You need to add a namespace
xmlns:yourapp="http://schemas.android.com/apk/res-auto"
Then
yourapp:showAsAction="always"
Edit:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_locate"
android:icon="#drawable/ic_action_location_found"
android:title="#string/locate"
yourapp:showAsAction="always" />
Quoting docs
Using XML attributes from the support library
Notice that the showAsAction attribute above uses a custom namespace
defined in the tag. This is necessary when using any XML
attributes defined by the support library, because these attributes do
not exist in the Android framework on older devices. So you must use
your own namespace as a prefix for all attributes defined by the
support library.
Try This :-
Menu Xml:
<item
android:id="#+id/action_location_found"
android:clickable="true"
android:icon="#drawable/more_btn"
android:showAsAction="always"
android:title="action_location_found">
<menu>
<item
android:id="#+id/action_user_profile"
android:orderInCategory="1"
android:showAsAction="never"
android:title="User Profile">
</item>
<item
android:id="#+id/action_settings"
android:orderInCategory="2"
android:showAsAction="never"
android:title="Settings">
</item>
</menu>
</item>
In your Activity:-
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action buttons
switch (item.getItemId()) {
case R.id.action_user_profile:
Toast.makeText(getApplicationContext(), "User Profile",
Toast.LENGTH_LONG).show();
break;
case R.id.action_settings:
Toast.makeText(getApplicationContext(), "Setting",
Toast.LENGTH_LONG).show();
break;
default:
return true;
}
return super.onOptionsItemSelected(item);
}
While i m trying to show 3 menu in overflow menu on sherlockactionbar but it not showing overflow icon , but when i press menu button from hardware it shows option at bottom of screen :
I m overriding menu through this in SherlockFragmentActivity
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.activity_home, menu);
return true;
}
and in Menu xml i have also added android:showAsAction="never" property coding for that .xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_settings"
android:icon="#android:drawable/ic_menu_info_details"
android:showAsAction="never"
android:title="about"/>
<item
android:id="#+id/menu_settings2"
android:icon="#drawable/plusnew"
android:showAsAction="never"
android:title="Add"/>
<item
android:id="#+id/menu_settings3"
android:icon="#drawable/tem2"
android:showAsAction="never"
android:title="Done"/>
</menu>
If your Phone has a HardwareButton for the Overflowmenu, it wont display the software button because you don't need it.
In this Case:
Not a Bug, its a feature :)
Try,
public boolean onCreateOptionsMenu(Menu menu) {
// Used to put dark icons on light action bar
SubMenu subMenu1 = menu.addSubMenu("Share");
subMenu1.add("Facebook").setOnMenuItemClickListener(
new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
return false;
}
});
MenuItem subMenu1Item = subMenu1.getItem();
subMenu1Item.setIcon(R.drawable.ic_share);
subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS
| MenuItem.SHOW_AS_ACTION_WITH_TEXT); //Note the flag SHOW_AS_ACTION_ALWAYS
return true;
}