menu not adding in overflow menu in sherlock action bar - android

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;
}

Related

why does not appear my menu icon on action bar?

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.

Action mode submenu and checkableBehavior

I have a strange issue with submenus and android:checkableBehavior="single". It works fine if the menu is in action bar, but displays check boxes instead of radio buttons if menu is in the action mode. I use AppCompatActivity and create action mode with startActionMode().
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/priority"
android:title="#string/priority"
app:showAsAction="ifRoom">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/low_priority"
android:title="#string/low_pririty"/>
<item
android:id="#+id/normal_priority"
android:title="#string/normal_priority"/>
<item
android:id="#+id/high_priority"
android:title="#string/high_priority"/>
</group>
</menu>
</item>
</menu>
How can I fix this?
You menu works fine for me using the startSupportActionMode method instead of the startActionMode method. The startActionMode method should not be used when using the support library AppCompatActivity.
.startSupportActionMode(new android.support.v7.view.ActionMode.Callback() {
#Override
public boolean onCreateActionMode(android.support.v7.view.ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.test_menu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(android.support.v7.view.ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(android.support.v7.view.ActionMode mode, MenuItem item) {
return false;
}
#Override
public void onDestroyActionMode(android.support.v7.view.ActionMode mode) {
}
});

All icons of ActionMode Bar are not showing in Android?

I have created a menu for my actionmode bar with icons but not all menu are showing with icon in actionmode bar. This is my menu xml file.
<?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/menu_archive"
android:icon="#drawable/ic_action_file_archive"
android:orderInCategory="100"
android:title="#string/action_remove"
app:showAsAction="always" />
<item
android:id="#+id/menu_upload_to_cloud"
android:icon="#drawable/ic_action_file_cloud_upload"
android:orderInCategory="200"
android:title="#string/action_upload_to_cloud"
app:showAsAction="always" />
<item
android:id="#+id/menu_delete"
android:icon="#drawable/ic_action_file_delete"
android:orderInCategory="300"
android:title="#string/action_move_to_trash"
app:showAsAction="always" />
</menu>
This is my code for creating Actionmode Bar.
#Override
public boolean onCreateActionMode(android.support.v7.view.ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.menu_actionmode_device_documents, menu);
return true;
}
#Override
public boolean onPrepareActionMode(android.support.v7.view.ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(android.support.v7.view.ActionMode mode, MenuItem item) {
}
#Override
public void onDestroyActionMode(android.support.v7.view.ActionMode mode) {
this.actionMode = null;
}
This image is my output which is showing only one icon of menu but i want all other icons too.
This may be a little too late, but I'm putting this answer in case someone else runs into the same problem. It seems the system does not keep count of the app:showAsAction="always" attribute.
The soulution is to update the menus manually in onPrepareActionMode
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
menu.findItem(R.id.menu_archive).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.findItem(R.id.menu_upload_to_cloud).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.findItem(R.id.menu_delete).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
This seems odd but it works.

Overflow menu doesn't show - android

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.

Adding items to actionbar

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);
}

Categories

Resources