Display menu items on ActionBar - android

I want to display my menu items on ActionBar with android
I found some samples but It was not what I wanted
Menu should be similar picture below(Overflow Icon or text)
My menu.xml is :
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_refresh"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="#drawable/icon"
android:visible="true"
android:title="Refresh"/>
<item
android:id="#+id/action_settings"
android:title="Settings">
<item
android:id="#+id/mapMenu"
android:title="map menu" />
<item
android:id="#+id/favMenu"
android:title="favorite" />
<item
android:id="#+id/listMenu"
android:title="List Menu" />
</item>
and my android code :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(getApplicationContext(), item.getTitle() + " selected", Toast.LENGTH_SHORT).show();
switch (item.getItemId()) {
case R.id.mapMenu:
// do something
break;
case R.id.favMenu:
// do something
break;
case R.id.listMenu:
// do something
break;
}
return true;
}

Once write following in all item tags
android:showAsAction="never"
then all items will display in overflow.
Ex:
<item
android:id="#+id/mapMenu"
android:showAsAction="never"
android:title="map menu" />
Hope this will helps you.

If you want to show the three dots, irrespective of device menu button, then you can call below code (which is based on reflection) in your application class' onCreate method-
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}
Caution: This is an awful hack that breaks consistency with the rest
of the apps on the platform

Thanks all
but I added android:showAsAction="ifRoom|withText" and result is :
I also tested android:showAsAction="ifRoom|withText" and menu items hid

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/main_menu"
android:showAsAction="always"
android:icon="#drawable/icon"
android:visible="true">
<item
android:id="#+id/action_refresh"
android:showAsAction="never"
android:title="Refresh"/>
<item
android:id="#+id/action_settings"
android:title="Settings"
android:showAsAction="never" />
<item
android:id="#+id/mapMenu"
android:title="map menu"
android:showAsAction="never" />
<item
android:id="#+id/favMenu"
android:title="favorite"
android:showAsAction="never" />
<item
android:id="#+id/listMenu"
android:title="List Menu"
android:showAsAction="never" />
</item>
</menu>
android:showAsAction: When and how this item should appear as an action item in the Action Bar. A menu item can appear as an action item only when the activity includes an ActionBar (introduced in API Level 11).
Try the given code. First you create root item keep its android:showAsAction:Always then inside that root item create other items with android:showAsAction:never.

Related

Toolbar icons disappearing after expanding the SearchView in Android

here's my problem:
I have that nice toolbar with the icons in landscape mode:
after expanding the search view and showing the popup menu the "add" item appears (I thought that it shouldn't):
then returning with the back arrow key, as you see, the add button goes:
and you won't find it in the popup menu anymore:
I'm using support:appcompat-v7:25.1.0, and here's my menu code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/app_bar_search"
android:actionViewClass="android.support.v7.widget.SearchView"
android:icon="#drawable/ic_action_search"
android:title="Search"
app:showAsAction="always|collapseActionView"
android:enabled="true"
android:visible="true"
app:actionViewClass="android.support.v7.widget.SearchView"/>
<item android:title="Add"
android:enabled="true"
android:icon="#drawable/ic_action_add"
android:visible="true"
app:showAsAction="ifRoom"
android:id="#+id/add" />
<item android:title="Settings"
android:id="#+id/settings"
app:showAsAction="never"
android:icon="#drawable/ic_action_settings"
android:enabled="true"
android:visible="true" />
<item android:title="Feedback"
android:id="#+id/feedbvack"
app:showAsAction="never"
android:icon="#drawable/ic_action_feedback"
android:enabled="true"
android:visible="true" />
</menu>
I can set the add button showAsAction to "always" but I know that this is discouraged.
Does anyone here know why is there this behavior? and how can I prevent that?
Thanks in advance.
You can try to use this:
MenuItemCompat.setOnActionExpandListener(searchMenuItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
supportInvalidateOptionsMenu();
//or use invalidateOptionsMenu();
return true;
}
});
So when SearchView will be collapsed Toolbar will reallocate items and ifRoom items will be visible. I had this bug too and solved it this way.
Try this
<item
android:id="#+id/app_bar_search"
android:actionViewClass="android.support.v7.widget.SearchView"
android:icon="#drawable/ic_action_search"
android:title="Search"
app:showAsAction="always|collapseActionView"
android:enabled="true"
android:visible="true"
app:actionViewClass="android.support.v7.widget.SearchView"/>
<item android:title="Add"
android:enabled="true"
android:icon="#drawable/ic_action_add"
android:visible="true"
app:showAsAction="always"
android:id="#+id/add" />
<item android:title="Settings"
android:id="#+id/settings"
app:showAsAction="never"
android:icon="#drawable/ic_action_settings"
android:enabled="true"
android:visible="true" />
<item android:title="Feedback"
android:id="#+id/feedbvack"
app:showAsAction="never"
android:icon="#drawable/ic_action_feedback"
android:enabled="true"
android:visible="true" />
Set app:showAsAction to always to make sure it will be visible always.
Use of ifRoom has this feature if space available it will show or else it will hide it better use always or never
<item android:title="Add"
android:enabled="true"
android:icon="#drawable/ic_action_add"
android:visible="true"
app:showAsAction="always"
android:id="#+id/add" />
#Kovy has answered above with the fix for this bug and I confirm it fixes the bug. Thank you very much, mate! However, the above function has been deprecated in favour of individual menu items OnActionExpandListener. Example of it is as posted below:
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
supportInvalidateOptionsMenu();
return true;
}
});

Action Bar Menu Items Duplicating on Run

I am very new to Java Programming and Android Studio.
While creating an Options Menu, I created a list of options in the main menu with two items in a group sub menu.
This is my menu.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/giveup_setting"
android:orderInCategory="1"
app:showAsAction="ifRoom"
android:title="#string/action_option1"/>
<item
android:id="#+id/new_game_setting"
android:orderInCategory="2"
app:showAsAction="never"
android:title="#string/action_option2"/>
<item
android:id="#+id/help_setting"
android:orderInCategory="3"
app:showAsAction="never"
android:title="#string/action_option3"/>
<item
android:id="#+id/settings_setting"
android:orderInCategory="4"
app:showAsAction="never"
android:title="#string/action_option4" />
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/menu_color"
android:orderInCategory="1"
app:showAsAction="never"
android:title="#string/layout_color"/>
<item
android:id="#+id/menu_text"
android:orderInCategory="2"
app:showAsAction="never"
android:title="#string/layout_text"/>
</group>
</menu>
<item
android:id="#+id/about_setting"
android:orderInCategory="5"
app:showAsAction="never"
android:title="#string/action_option5"/>
<item
android:id="#+id/exit_setting"
android:orderInCategory="6"
app:showAsAction="never"
android:title="#string/action_option6"/>
</menu>
An this is how I inflate the menu in my MainActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
menu.clear();
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem menu) {
// Handle item selection
switch (menu.getItemId()) {
case R.id.help_setting:
//show help screens
return true;
case R.id.menu_color:
//change a layout of my main activity
return true;
case R.id.menu_text:
//change another layout of my main activity
return true;*/
case R.id.about_setting:
//show my about screen
return true;
case R.id.giveup_setting:
//resets the game
case R.id.new_game_setting:
//restarts the app
return true;
case R.id.exit_setting:
//exits the app
return true;
default:
return super.onOptionsItemSelected(menu);
}
}
On running the app, the menu populates but the "Settings" item (which has the nested group radio buttons) displays twice.
On running the app, the menu populates but the "Settings" item (which has the nested group radio buttons) displays twice.
What am I doing wrong?
if android:id="#+id/settings_setting" is the one you want to have a submenu don't close the item tag. Instead inflate <menu> inside the <item> tag.
<item
android:id="#+id/settings_setting"
android:orderInCategory="4"
app:showAsAction="never"
android:title="#string/action_option4">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/menu_color"
android:orderInCategory="1"
app:showAsAction="never"
android:title="#string/layout_color"/>
<item
android:id="#+id/menu_text"
android:orderInCategory="2"
app:showAsAction="never"
android:title="#string/layout_text"/>
</group>
</menu>
</item>

Is a scollable TextSelectionMenu possible?

It seems impossible to find any answer on my question/problem regarding af scollable TextSelectionMenu! I have searched the whole Stackoverflow and Google without any succes and I have tried various ideas out.
I have 10 menu items in my custom TextSelectionMenu. Only 6 of them is visible, the rest is of course not, since you can't scroll to the others. Is it possible to implement a Scrollview/HorizontalListview of any sort to be able to scroll to the rest?
My menu_main.xml:
<?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/textcolor"
android:icon="#drawable/textcolor"
android:showAsAction="always"
android:visible="true"
android:title="ColorPicker"
tools:ignore="AppCompatResource" />
<item
android:id="#+id/bold"
android:icon="#drawable/bold2"
android:showAsAction="always"
android:visible="true"
android:title="Bold"
tools:ignore="AppCompatResource" />
<item
android:visible="true"
android:id="#+id/italic"
android:icon="#drawable/italic2"
android:showAsAction="always"
android:title="Italic"
tools:ignore="AppCompatResource" />
<item
android:visible="true"
android:id="#+id/underline"
android:icon="#drawable/underline2"
android:showAsAction="always"
android:title="Underline"
tools:ignore="AppCompatResource" />
<item
android:visible="true"
android:id="#+id/stroke"
android:icon="#drawable/strike"
android:showAsAction="always"
android:title="Strikethrough"
tools:ignore="AppCompatResource" />
<item
android:visible="true"
android:id="#+id/increase"
android:icon="#drawable/increase"
android:showAsAction="always"
android:title="Increase"
tools:ignore="AppCompatResource" />
<item
android:visible="true"
android:id="#+id/decrease"
android:icon="#drawable/decrease"
android:showAsAction="always"
android:title="Decrease"
tools:ignore="AppCompatResource" />
The CustomTextSelectionMenu class:
public class CustomTextSelectionMenu extends FragmentActivity implements android.view.ActionMode.Callback {
EditText editText = (EditText) findViewById(R.id.edittext);
#Override
public boolean onCreateActionMode(android.view.ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
menu.removeItem(android.R.id.selectAll);
menu.removeItem(android.R.id.paste);
return true;
}
#Override
public boolean onPrepareActionMode(android.view.ActionMode mode, Menu menu) {
return true;
}
#Override
public boolean onActionItemClicked(android.view.ActionMode mode, MenuItem item) {
// Just a lot of switch cases inside here...
return true;
}
}
I think, there is no way to scroll the text selection menu.
But you can do is, show the few menu items say 2 or 3 as showAsAction="always" and the rest of the menu items are automatically can be shown in overflow menu.

How to Show Icon with Menus in android

I want to show icon in Option Menus. I tried many methods but I can't find success.In my toolbar some icon already put but i want icon in my Option menu.
I want same as below image:-
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"
tools:context=".MainActivity">
<item
android:id="#+id/action_search"
android:title="#string/action_search"
android:orderInCategory="1"
android:icon="#drawable/search"
app:showAsAction="ifRoom" />
<item
android:id="#+id/action_cart"
android:title="#string/action_search"
android:orderInCategory="2"
android:icon="#drawable/shoppingcart"
android:actionLayout="#layout/feed_update_count"
app:showAsAction="ifRoom" />
<item
android:id="#+id/menu_overflow"
android:title="#string/my_cart"
android:icon="#drawable/ic"
android:orderInCategory="11111"
app:showAsAction="always">
<menu>
<item
android:id="#+id/login"
android:title="#string/login"
android:orderInCategory="1"
android:icon="#drawable/account"
app:showAsAction="always|withText"/>
<item
android:id="#+id/my_order"
android:title="#string/my_order"
android:orderInCategory="2"
android:icon="#drawable/track"
app:showAsAction="withText"/>
<item
android:id="#+id/wishlist"
android:title="#string/wishlist"
android:orderInCategory="3"
android:icon="#drawable/wishlist"
app:showAsAction="withText"/>
</menu>
</item>
</menu>
I am new in android developing anyone can help me how to show icon in option menu. Thanks in advance!
By default the overflow menu will disable the icon and only show text, you need some workaround.
1st way
You can enable it by adding the following codes into your class
#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(TAG, "onMenuOpened", e);
}
catch(Exception e){
throw new RuntimeException(e);
}
}
}
return super.onMenuOpened(featureId, menu);
}
2nd way
Create your own overflow item and a nested menu to show both the icon and the text.
Example,
<item
android:id="#+id/overflow"
android:icon="#drawable/ic_overflow_white"
android:orderInCategory="201"
android:title="#string/overflow"
app:showAsAction="always">
<menu>
<item
android:id="#+id/cab_menu_select_all"
android:icon="#drawable/ic_select_all_grey"
android:orderInCategory="100"
android:title="#string/cab_menu_select_all"
app:showAsAction="always|withText"></item>
</menu>
</item>
try this code its works for you
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:showAsAction="always"
android:title="#string/action_search"/>
<!-- Settings, should always be in the overflow -->
<item
android:id="#+id/action_location_found"
android:icon="#drawable/ic_action_location_found"
android:showAsAction="always"
android:title="#string/action_location_found"/>
<item
android:id="#+id/menu_overflow"
android:icon="#drawable/ic_action_menu"
android:orderInCategory="11111"
android:showAsAction="always">
<menu>
<!-- Refresh -->
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_action_refresh"
android:showAsAction="ifRoom"
android:title="#string/action_refresh"/>
<!-- Help -->
<item
android:id="#+id/action_help"
android:icon="#drawable/ic_action_help"
android:showAsAction="never"
android:title="#string/action_help"/>
<!-- Check updates -->
<item
android:id="#+id/action_check_updates"
android:icon="#drawable/ic_action_refresh"
android:showAsAction="never"
android:title="#string/action_check_updates"/>
</menu>
</item>
</menu>
- See more at: http://www.theappguruz.com/blog/android-working-android-actionbar#sthash.ArNQTEBF.dpuf
Create your menu xml as follow
<item
android:id="#+id/menu_item_action_parameters"
android:title="#string/text_parameters"
android:icon="#drawable/ic_menu_parameter"
app:showAsAction="ifRoom|withText"/> >
<menu>
<item
android:id="#+id/action_dropdown1"
android:title="#string/dropdown_1" />
<item
android:id="#+id/action_dropdown2"
android:title="#string/dropdown2" />
<item
android:id="#+id/action_dropdown3"
android:title="#string/dropdown3" />
</menu>
</item>
<item
more item
</item>
Then
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_dropdown1:
.....
return true;
case R.id.action_dropdown2:
.....
return true;
.....
default:
return super.onOptionsItemSelected(item);
}
}
this is completely working code.Mark this answer up if it helps. :) Happy coding.
you Need to add tools:context="your class" to menu tag
<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"
tools:context=".activities.BaseActivity">
<item
android:id="#+id/action_notification1"
android:icon="#drawable/one"
android:title="action_notification"
app:showAsAction="always"/>
<item
android:id="#+id/action_notification2"
android:icon="#drawable/two"
android:title="action_notification"
app:showAsAction="always"/>
<item
android:id="#+id/action_notification3"
android:icon="#drawable/three"
android:title="action_notification"
app:showAsAction="always"/>
</menu>
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
you shold write tool:context to menu tag then run you will get icons to your text

Action Overflow is not working

I have tried everything that i could have. gone through 6-7 articles except http://developer.android.com/ and things totally messed up i don't know what i am missing or taking the reference of wrong articles.
Here is my code:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/userIcon"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="#drawable/icon_user"
android:title="#string/app_name"/>
<item
android:id="#+id/listIcon"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="#drawable/icon_list"
android:title="#string/app_name"/>
<item
android:id="#+id/menu_red"
android:orderInCategory="1"
android:showAsAction="ifRoom|withText"
android:title="#string/get_back"/>
<item
android:id="#+id/menu_green"
android:orderInCategory="2"
android:showAsAction="ifRoom|withText"
android:title="#string/get_login"/>
</menu>
Activity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_product_list, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.listIcon:
Toast.makeText(this,
"Menu list",
Toast.LENGTH_LONG).show();
break;
case R.id.userIcon:
Toast.makeText(this,
"User Icon",
Toast.LENGTH_LONG).show();
break;
}
//return super.onOptionsItemSelected(item);
return true;
}
Thanks a lot
Order in category determines the order appearance, from low to high check this link -> Set Item postition in ActionBar try the following code, but i'm not sure about in which order you want the menus to appear
<item
android:id="#+id/userIcon"
android:orderInCategory="1"
android:showAsAction="always"
android:menuCategory="system"
android:icon="#drawable/icon_user"
android:title="#string/app_name"/>
<item
android:id="#+id/listIcon"
android:orderInCategory="2"
android:showAsAction="always"
android:menuCategory="system"
android:icon="#drawable/icon_list"
android:title="#string/app_name"/>
<item
android:id="#+id/menu_red"
android:orderInCategory="99"
android:showAsAction="ifRoom|withText"
android:title="#string/get_back"/>
<item
android:id="#+id/menu_green"
android:orderInCategory="100"
android:showAsAction="ifRoom|withText"
android:title="#string/get_login"/>

Categories

Resources