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!
Related
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) {
}
});
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.
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>
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;
}
At my new application that I created, I got auto generated code for creating menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
And I added item at menu.xml
<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/item1"></item>
</menu>
But there is no menu button, am I missing something?
EDIT:
In your menu definition you put:
android:showAsAction="never"
Change it to:
android:showAsAction="always"
Check this example, including an icon for the menu:
<item
android:id="#+id/menu_calendar"
android:title="#string/calendar"
android:icon="#drawable/ic_menu_calendar_holo_light"
android:showAsAction="always|withText" />
always means the button will always be displayed. You can replace it by ifRoom if it is a menu such as Settings that should appear in the menu as an option but not displayed all the time.
withtext means that the title of the menu will be displayed beside the icon if there is enough place for it.
Details about all these options are available here.
For the rest, you need to create and show the ActionBar in your onCreate() function:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//you might need this line if you are not using the Holo theme
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.main_activity);
ActionBar actionBar = getActionBar();
actionBar.show();
(...)
Check for the import you are using. Check example below
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
You should use Menu and MenuInflater of actionbarsherlock..
Check out this link on implemention of ActionBarSherlock
Try
<item
android:id="#+id/action_settings"
android:showAsAction="always"
android:title="#string/action_settings"/>
<item android:id="#+id/item1"></item>