Can you please tell me how can I create an android menu item with icon.
Im insert image from menu main.xml but not working
<item
android:id="#+id/menu_settings"
android:icon="#drawable/ic_launcher"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item
android:id="#+id/menu_cut"
android:icon="#drawable/ic_launcher"
android:orderInCategory="100"
android:showAsAction="never"
android:title="cut"/>
<item
android:id="#+id/menu_copy"
android:orderInCategory="100"
android:showAsAction="never"
android:title="copy"/>
<item
android:id="#+id/menu_past"
android:orderInCategory="100"
android:showAsAction="never"
android:title="past"/>
You need to set showAsAction as ifRoom|withText
<item
android:icon="#drawable/img"
android:title="title"
android:showAsAction="ifRoom|withText" />
Icons are used for toolbar buttons. You have android:showAsAction="never", forcing this item into the overflow. The overflow does not use icons.
programmatically set the icon like this:
menu.add(0, MENU_QUIT, 0, "Quit").setIcon(R.drawable.menu_quit_icon);
or You can set it in your xml layout like this:
<item android:id="#+id/save_button"
android:icon="#android:drawable/ic_menu_save"
android:title="Save Image"/>
It's not supported in Android 3.0+ http://android-developers.blogspot.in/2012/01/say-goodbye-to-menu-button.html
Help this helps.
Try this will help you to understand menu
Related
Noob question. I'm using Toolbar and menu in Android app. How i can to change margin or padding between two menu items?
All answers i found is about Sharlock bar (https://stackoverflow.com/questions/15678467/how-to-add-padding-between-menu-items-in-android) or old Action bar (https://stackoverflow.com/questions/21131814/how-to-padding-between-actionbars-menu-items-icon-when-using-the-support-librare).
I think the answer near with style files, but i realy stack what parent or item name i need for my case.
My sample menu:
<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/menu_contents"
android:title="#string/menu_contents"
android:icon="#drawable/ic_format_list_numbered"
android:orderInCategory="2"
app:showAsAction="always" />
<item android:id="#+id/menu_search"
android:title="#string/menu_search"
android:icon="#drawable/ic_search"
android:orderInCategory="100"
app:showAsAction="always" />
<item android:id="#+id/menu_settings"
android:title="#string/menu_settings"
android:orderInCategory="100"
app:showAsAction="never" />
Thank you!
You'll want to programmatically assign an ActionProvider using setActionProvider() on the MenuItem that gets created for the items you want to change.
How to add a text view in my Action bar to show data in android application.
Following is my menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<menu>
<item
android:id="#+id/action_log"
android:title="#string/action_log"
android:showAsAction="never"/>
<item
android:id="#+id/account_log"
android:title="#string/accountlog"
android:showAsAction="never"/>
<item android:id="#+id/logout"
android:title="Logout"
android:showAsAction="never"/>
</menu>
</menu>
Any changes required in strings.xml?
Change
android:showAsAction="never"
to
android:showAsAction="ifRoom"
Please check the correct case of ifRoom, I am not quite sure.
Solved by Using custom action bar Thank you all
I am unable to get the icon to show with or without the title text using the AppCompat Toolbar. The title text appears fine, but there is no icon shown at all.
The icon and title text are defined in a menu.xml using the following xml:
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:context="ui.CreateProfileActivity">
<item
android:id="#+id/create_profile_submit"
android:title="#string/create_profile_submit"
app:icon="#drawable/ic_done_white_24dp"
app:showAsAction="always"/>
</menu>
In my styles.xml I've set the icon color to be white explicitly (the background of the Toolbar is blue):
<item name="actionBarIconColor">#fff</item>
This is the toolbar_actionbar.xml that is used to display the Toolbar:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:theme="#style/ActionBarThemeOverlay"
app:popupTheme="#style/ActionBarPopupThemeOverlay"
android:id="#+id/toolbar_actionbar"
android:background="?attr/colorPrimaryDark"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
When I launch the application in either portrait or landscape, the icon does not appear.
How do I get the Toolbar to either show:
The icon and text
Only the icon (See Edit Below)
EDIT: 2014.12.14
I was able to get the icon, and only the icon, to show by updating the menu.xml "icon" property to use "android" instead of "app". The new xml is as follows:
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:context="ui.CreateProfileActivity">
<item
android:id="#+id/create_profile_submit"
android:title="#string/create_profile_submit"
android:icon="#drawable/ic_done_white_24dp"
app:showAsAction="always"/>
</menu>
I am still unable to get the icon AND text to show at the same time in portrait mode. It does show both in landscape mode.
Get the reference of the toolbar within your activity and set the Icon or most of its parts
ToolBar toolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
toolbar.setLogo(R.drawable.yourdrawable);
toolbar.setTitle("Your Title");
toolbar.setSubtitle("list");
toolbar.setNavigationIcon(R.drawable.drawer_icon);
I know this is beyond what is requested of the question, buh i wanted to shed more light on what you can
Try out to solve the problem.
I hope this helps somebody.
try this menu: for toolbar
`
<item
android:id="#+id/menu_overflow"
android:enabled="true"
android:icon="#drawable/ic_menu_moreoverflow_mtrl_alpha"
app:showAsAction="always"
android:title="">
<menu>
<item
android:id="#+id/action_more"
android:icon="#drawable/ic_more_apps"
android:orderInCategory="100"
android:showAsAction="never"
android:title="More apps"/>
<item
android:id="#+id/action_rate"
android:icon="#drawable/ic_rating"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Rate this app"/>
<item
android:id="#+id/action_feedback"
android:icon="#drawable/ic_feedback"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Feedback"/>
<item
android:id="#+id/action_share"
android:icon="#drawable/ic_share"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Share this app"/>
</menu>
</item>
`
try this menu for actionbar
`
<item
android:id="#+id/menu_overflow"
android:enabled="true"
android:icon="#drawable/ic_menu_moreoverflow_mtrl_alpha"
android:showAsAction="always"
android:title="">
<menu>
<item
android:id="#+id/action_more"
android:icon="#drawable/ic_more_apps"
android:orderInCategory="100"
android:showAsAction="never"
android:title="More apps"/>
<item
android:id="#+id/action_rate"
android:icon="#drawable/ic_rating"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Rate this app"/>
<item
android:id="#+id/action_feedback"
android:icon="#drawable/ic_feedback"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Feedback"/>
<item
android:id="#+id/action_share"
android:icon="#drawable/ic_share"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Share this app"/>
</menu>
</item>
`
I think this is so late. But this code works well. You can try this.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (menu instanceof MenuBuilder) {
((MenuBuilder) menu).setOptionalIconsVisible(true);
}
getMenuInflater().inflate(R.menu.menu_quran, menu);
return true;
}
I want to display overflow menu in android actionbar.
In all device i need same design app name at left side of actionbar and overflow icon at right side with menu in it.
Currently when i run app menu are showing on clicking of menu icon.
I just want to show menu like attached image-
below is my code inside menu.xml -
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_red"
android:orderInCategory="1"
android:showAsAction="never"
android:title="#string/news"/>
<item
android:id="#+id/menu_green"
android:orderInCategory="2"
android:showAsAction="never"
android:title="#string/online_test"/>
how to show menu in such way.
Thanks in advance.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="always"
<menu>
<item
android:id="#+id/menu_red"
android:title="red"/>
<item
android:id="#+id/menu_green"
android:title="green"/>
</menu>
</item>
this worked for me maybe work for you too
I know two ways to show action bar icons. My minimum sdk is 14 and target sdk 19. I am debugging with android 4.2.
First one
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/mPre"
android:icon="#drawable/ic_action_previous_item"
android:showAsAction="always"
android:title="Previous"/>
<item
android:id="#+id/mNext"
android:icon="#drawable/ic_action_next_item"
android:showAsAction="ifRoom"
android:title="Next"/>
<item
android:id="#+id/mShare"
android:icon="#drawable/ic_action_share"
android:showAsAction="ifRoom"
android:title="Share"/>
<item
android:id="#+id/mFb"
android:icon="#drawable/fb"
android:showAsAction="ifRoom"
android:title="Facebook"/>
<item
android:id="#+id/mhelp"
android:icon="#drawable/ic_action_help"
android:showAsAction="ifRoom"
android:title="Help"/>
</menu>
This will show icon dynamically (2 or 3 as consider space of action bar). But problem is, this is not showing extra menu in overflow icon. When I touch my phone menu bar, extra menu will appears, but I want extra menu in overflow icon.
For solution my second code.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/mPre"
android:icon="#drawable/ic_action_previous_item"
android:showAsAction="always"
android:title="Previous"/>
<item
android:id="#+id/mNext"
android:icon="#drawable/ic_action_next_item"
android:showAsAction="always"
android:title="Next"/>
<item
android:id="#+id/mMore"
android:icon="#drawable/ic_action_overflow"
android:showAsAction="always"
android:title="More">
<menu>
<item
android:id="#+id/mShare"
android:icon="#drawable/ic_action_share"
android:showAsAction="ifRoom"
android:title="Share"/>
<item
android:id="#+id/mFb"
android:icon="#drawable/fb"
android:showAsAction="never"
android:title="Facebook"/>
<item
android:id="#+id/mhelp"
android:icon="#drawable/ic_action_help"
android:showAsAction="never"
android:title="Help"/>
</menu>
</item>
</menu>
The problem of this code this is not really dynamic. I am showing here two icon always and put others icon in overflow icon, this overflow icon is not real overflow icon.
Now what I want?
I want if I have five menu as much as menu will show in action bar and rest of the icons are stored in overflow icon automatically.
I hope you guys get my problem. Please help me. Thanks in advance.
i think you need to use this
android:showAsAction="ifRoom|withText"
the parameter showAsAction decides where the icon is to be shown
refer to this link and the documentation
If I understand correctly, you want the the action bar icons to always show on the overflow? If that's the case, you can try the following:
<item
android:id="#+id/action_settings"
android:title="Test"
app:showAsAction="never" />