<?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/item1"
android:title="#string/item_name_1"
app:showAsAction="ifRoom"
android:orderInCategory="3" />
<item
android:id="#+id/item2"
android:title="Create"
app:showAsAction="ifRoom"
android:icon="#drawable/create"
/>
<item
android:id="#+id/item3"
android:title="Delete"
app:showAsAction="always"
android:icon="#drawable/delete"
/>
</menu>
I am trying to use orderIncategory attribute of item tag in menu but it is not wrking as no changes are getting reflected. Am I doing anything wrong
The order in which the menu items will appear within the menu. The menu items are arranged from left to right in ascending order of integer value of orderInCategory (i.e. 1,2,3... -> left to right).
The items with the lowest orderInCategory values are displayed as actions, and the remaining items are displayed in the overflow menu.
Related
Here is my menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
tools:ignore="HardcodedText"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/general_settings"
android:title="Settings" />
<item
android:id="#+id/settings_alert"
android:title="Profile List" />
<item
android:id="#+id/settings_alarm"
android:title="Alarm List" />
<item
android:id="#+id/settings_apps"
android:title="App List" />
</menu>
There is no android:background in menu. I want to be able to choose a menu option and have it show a different background color until the next choice is made.
I am able to use android:isCheckable=true, but then it shows checkboxes which I don't want.
I have also tried working with onPrepareOptionsMenu and onMenuOpened, but setBackgroundColor isn't a attribute.
Background
Just want to see how to make action items in code without XML
The problem
I've come to some case that I fail to see how I can create in code.
The case below is showing a normal action-item that has an icon, and when pressed, it shows a sub-menu of items to choose from. I want to create the same, but programmatically:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:icon="#android:drawable/ic_menu_info_details" android:title="More info" app:showAsAction="always">
<menu>
<group android:checkableBehavior="none" android:menuCategory="container">
<item android:id="#+id/..." android:title="item 1"/>
<item android:id="#+id/..." android:title="item 2"/>
<item android:id="#+id/..." android:title="item 3"/>
</group>
</menu>
</item>
</menu>
When using this, the result is:
And after clicking:
What I've tried
I tried 2 solutions:
Create a subMenu (using addSubMenu), and then setting an action-item properties to it.
Create a normal Action item (using add) and then adding sub-items to it.
The result for both of them failed in comparison to what
The question
How can I make the exact same thing programmatically, without using XML resource for it.
I have created the following top-level main Menu in the Android app:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<group android:id="#+id/group_speed" android:checkableBehavior="single" >
<item android:id="#+id/slow" android:title="#string/speed_slow"/>
<item android:id="#+id/normal" android:title="#string/speed_normal"/>
<item android:id="#+id/fast" android:title="#string/speed_fast" />
</group>
<group android:id="#+id/group_info" android:checkableBehavior="none">
<item android:id="#+id/help" android:title="Help"/>
<item android:id="#+id/about" android:title="About"/>
</group>
</menu>
I would like to know if there is a simple way to add a Horizontal divider between two menu groups (and/or two menu items within the group) in a declarative way using just XML statement (optionally, adding shape resources, attributes, etc., but not using java code for this decorative task).
I am a beginner in Espresso. I have this 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/configuration"
android:icon="#drawable/ic_settings"
android:title="Configuration"
app:showAsAction="ifRoom">
<menu>
<item
android:id="#+id/add_sound"
android:title="Add a sound"
app:showAsAction="ifRoom" />
<item
android:id="#+id/takeof_sound"
android:enabled="false"
android:title="Take of the sound"
app:showAsAction="ifRoom" />
<item
android:id="#+id/add_image"
android:title="Add an image"
app:showAsAction="ifRoom" />
<item
android:id="#+id/takeof_image"
android:enabled="false"
android:title="Take of the image"
app:showAsAction="ifRoom" />
</menu>
</item>
<item
android:id="#+id/add"
android:icon="#drawable/ic_add"
android:title="Add"
app:showAsAction="ifRoom" />
</menu>
I would like to perform a click on the item with id configuration and then a click on the sub-item with id add_sound. So, I have typed this code:
public void menuConfigurationTest()
{
onView(withId(R.id.configuration)).perform(click());
onView(withId(R.id.add_sound)).perform(click());
}
However I get this error:
android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: com.example.adrien.smartalarm:id/add_sound
If the target view is not part of the view hierarchy, you may need to use Espresso.onData to load it from one of the following AdapterViews:android.support.v7.widget.MenuPopupWindow$MenuDropDownListView{5a0c4d8 VFED.VC.. .F...... 0,0-686,672}
What is wrong with what I have done?
The problem is the submenu gets shown in a PopupWindow, which is not part of activity's view hierarchy. Therefore you have to add:
.inRoot(RootMatchers.isPlatformPopup())
The next thing is the items are displayed in a special ListView named MenuDropDownListView. So onView() will not work here, you have to use onData().
Therefore the complete expression is:
onData(CoreMatchers.anything())
.inRoot(RootMatchers.isPlatformPopup()) // isPlatformPopup() == is in PopupWindow
.inAdapterView(CoreMatchers.<View>instanceOf(MenuPopupWindow.MenuDropDownListView.class))
.atPosition(0) // for the first submenu item, here: add_sound
.perform(click());
I've an action bar and three tabs each includes some list to display.
When I click search I want the rest of the action bar should be hidden except the list in a tab and search item it self should be enabled, where want to search.
How can I achieve it?
Edit:
<?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">
<!-- Single menu item
Set id, icon and Title for each menu item
-->
<item
android:id="#+id/action_search"
android:title="#string/action_search"
android:orderInCategory="100"
android:icon="#drawable/ic_action_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
<item android:id="#+id/home"
android:icon="#drawable/icon_home"
android:title="Home" />
<item android:id="#+id/totalCash"
android:icon="#drawable/icon_delivered"
android:title="Cod" />
<item android:id="#+id/statusForEach"
android:icon="#drawable/icon_cancelled"
android:title="Status" />
<item android:id="#+id/logout4"
android:icon="#drawable/icon_cancelled"
android:title="Logout" />
<!--<item android:id="#+id/menu_delete"
android:icon="#drawable/icon_delete"
android:title="Delete" />
<item android:id="#+id/menu_preferences"
android:icon="#drawable/icon_preferences"
android:title="Preferences" />-->
add below line to all menu items except search
app:showAsAction="ifRoom"
and in your search item add
app:iconifiedByDefault="true"
You have to implement MenuItemCompat interface to get those events. that will allow you to listen open and close and many more events. so check it.