I want to align one item to the left of ActionBar and others to the right like this :
I have 5 items in my menu xml.
main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="fr.mgs.consulting.allbrary_android.MainActivity" >
<item
android:id="#+id/action_spinner"
android:showAsAction="always"
android:actionLayout="#layout/spinner_actionbar"
android:title="#string/action_spinner"
android:orderInCategory="1"/>
<item
android:id="#+id/action_search"
android:showAsAction="always"
android:title="#string/action_spinner"
android:icon="#drawable/ic_action_search"
android:orderInCategory="2"/>
<item
android:id="#+id/action_tablet"
android:showAsAction="never"
android:title="#string/action_spinner"
android:icon="#drawable/ic_action_tablet"
android:orderInCategory="3"/>
<item
android:id="#+id/action_notifications"
android:showAsAction="always"
android:title="#string/action_spinner"
android:icon="#drawable/ic_action_notifications"
android:orderInCategory="4"/>
<item
android:id="#+id/action_user"
android:showAsAction="always"
android:title="#string/action_spinner"
android:icon="#drawable/ic_action_user"
android:orderInCategory="5"/>
How shall we do it?
Sorry for my very bad English.
You can create your own custom view for ActionBar where this left element will be like simple image view, and others menu items will be as usual menu items.
Example here: http://javatechig.com/android/actionbar-with-custom-view-example-in-android
Related
I am using Navigation Drawer menu without the header. The first item in the menu is too close to the top bar. How can I create margin for the first item? I am using this drawer menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Home"
android:id="#+id/nav_item_home"
android:icon="#drawable/ic_home_black_24dp"/>
<item android:title="Search Definitions"
android:id="#+id/nav_item_search"
android:icon="#drawable/ic_search_black_24dp"/>
<item android:title="About">
<menu>
<item android:title="About this app"
android:id="#+id/nav_item_about"
android:icon="#drawable/ic_info_black_24dp"/>
<item android:title="How to use this app"
android:id="#+id/nav_item_howtouse"
android:icon="#drawable/ic_perm_device_information_black_24dp"/>
</menu>
</item>
The outcome is this:
What I did as a workaround is to wrap the first item in a item menu with a blank title ... like this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="">
<menu>
<item android:title="Home"
android:id="#+id/nav_item_home"
android:icon="#drawable/ic_home_black_24dp"/>
<item android:title="Search Definitions"
android:id="#+id/nav_item_search"
android:icon="#drawable/ic_search_black_24dp"/>
</menu>
</item>
<item android:title="About">
<menu>
<item android:title="About this app"
android:id="#+id/nav_item_about"
android:icon="#drawable/ic_info_black_24dp"/>
<item android:title="How to use this app"
android:id="#+id/nav_item_howtouse"
android:icon="#drawable/ic_perm_device_information_black_24dp"/>
</menu>
</item>
And then the outcome is exactly what I wanted:
Can someone please confirm if this workaround is the only way or is there any correct way of achieving the desired output.
Bit late, but any solution?
I've just played around in the xml:
<android.support.design.widget.NavigationView>
...
android:layout_marginTop="?attr/actionBarSize"
android:paddingTop="somePaddingValueInSp"
...
/>
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.
My main file:
<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">
<menu>
<item
android:id="#+id/menu_adhoc1"
android:icon="#drawable/icon_custom"
android:title="#string/menuitem2_3"/>
<item
android:id="#+id/cPanel1"
android:icon="#drawable/icon_cpanel"
android:title="#string/menuitem2_5"/>
<item
android:id="#+id/tutorial1"
android:icon="#drawable/icon_tutorial"
android:title="#string/menuitem2_4"/>
</menu>
</item>
</menu>
What I am trying to achieve is that place three more options under the menu, however I get only one option under menu labelled as "Settings", clicking on this leads to the desired result of getting three options under the menu. Where am I going wrong, any hints?
The problem is that you nested a <menu> containing <item>s inside another <item>.
As per the Menu documentation, this adds your second menu as a submenu of the parent item.
What it sounds like you are looking for is all the items being withing the same menu, like so:
<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/menu_adhoc1"
android:icon="#drawable/icon_custom"
android:title="#string/menuitem2_3"/>
<item
android:id="#+id/cPanel1"
android:icon="#drawable/icon_cpanel"
android:title="#string/menuitem2_5"/>
<item
android:id="#+id/tutorial1"
android:icon="#drawable/icon_tutorial"
android:title="#string/menuitem2_4"/>
</menu>
I have used actionbar in my app. I have 3 icons on actionbar, but when the app runs it only shows 2 icons i.e. First and the last one, not mid one. I want to show all of them everytime.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<!-- Share, should appear as action button -->
<item
android:id="#+id/action_share"
android:icon="#drawable/ic_action_share"
android:showAsAction="ifRoom"
android:title="Generate QR"
yourapp:showAsAction="ifRoom"/>
**<!-- NOT SHOWING THIS ONE -->
<item
android:id="#+id/action_save"
android:icon="#drawable/ic_action_save"
android:showAsAction="ifRoom"
android:title="Generate QR"
yourapp:showAsAction="ifRoom"/>**
<item
android:id="#+id/action_nav_menu"
android:icon="#drawable/ic_action_overflow"
android:orderInCategory="100"
android:showAsAction="ifRoom"
android:title="Menu"
yourapp:showAsAction="always"/>
As you want all 3 items to be displayed every time, include yourapp:showAsAction="always" in each item.
I have set three action items which am adding to the ABS through menu XML like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_share_app"
android:icon="#drawable/share_ab"
android:showAsAction="collapseActionView"
android:title="#string/menu_share_app"/>
<item
android:id="#+id/menu_search"
android:icon="#drawable/share_ab"
android:showAsAction="always"
android:title="#string/menu_search"/>
<item
android:id="#+id/menu_settings"
android:icon="#drawable/share_ab"
android:showAsAction="collapseActionView"
android:title="#string/menu_settings"/>
</menu>
I want to show the search item alwys and the other two in a dropdown, I made the showasaction as "collapseActionView" but am not getting any dropdown. Here's the sample pic of what am expecting. Thanks! :)
I just changed the code and order.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_search"
android:icon="#drawable/share_ab"
android:showAsAction="always"
android:title="#string/menu_search"/>
<item
android:id="#+id/menu_share_app"
android:icon="#drawable/share_ab"
android:showAsAction="never"
android:title="#string/menu_share_app"/>
<item
android:id="#+id/menu_settings"
android:icon="#drawable/share_ab"
android:showAsAction="never"
android:title="#string/menu_settings"/>
</menu>
If you are having physical menu button (e.g. Nexus S) in your device then the dot line will not show. Even if your AVD emulates a device with has physical menu button then same thing will happen. When you press the Menu button it’ll show up.