How to keep options menu open in titlebar? - android

I need to create menu in titlebar, where user can chose many variants at one time. It will be a filter options of content in activity.
So, it should looks like this:
I can use standard menu items like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/one"
android:checkable="true"
android:checked="true"
android:title="#string/one"
app:showAsAction="never" />
<item
android:id="#+id/two"
android:checkable="true"
android:checked="true"
android:title="#string/two"
app:showAsAction="never" />
</menu>
And it will work perfectly, but after click on any item, menu will close.
I find old question about it: Android - keep options menu open .
May be already there are some variants to resolve it, without create menu by my own?
Or I can use spinner:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/one"
android:icon="#drawable/ic_arrow_dropdown"
app:actionViewClass="android.widget.Spinner"
app:showAsAction="always"
android:title="some title" />
</menu>
But Spinner doesn't support multiselect.
What is the right way to do it?

Related

Android MenuItem, Change background color on selected item

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.

Android How to move menu to the left on small screens

When i added additional item to menu, it go off the screen on phone with smaller screen like 400x800. I tried to add padding but i don't want to make icon smaller but only move whole menu a little bit to the left because i see there is space for that. Does anyone has similar problem and resolve it ?
code:
<?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/graphic"
android:icon="#drawable/ic_graphic_white_30dp"
android:title="Graphic"
app:showAsAction="always" />
<item
android:id="#+id/save"
android:icon="#drawable/ic_save_white_24dp"
android:title="#string/save"
app:showAsAction="always" />
<item
android:id="#+id/share"
android:icon="#drawable/ic_share_white_24dp"
android:title="#string/share"
app:showAsAction="always" />
<item
android:id="#+id/count"
android:icon="#drawable/ic_pie_chart_white_24dp"
android:title="#string/count"
app:showAsAction="always" />
<item
android:id="#+id/camera"
android:icon="#drawable/ic_camera_alt_white_24dp"
android:title="#string/photo"
app:showAsAction="always" />
<item
android:id="#+id/lock"
android:icon="#drawable/ic_action_secure"
android:title="#string/lock"
app:showAsAction="always" />
</menu>
You can use configrations of your actionBar as below:
ActionBar mActionBar = getSupportActionBar();
mActionBar.setNavigationMode(NAVIGATION_MODE_LIST);
mActionBar.setListNavigationCallbacks(mAdapter, mNavigationCallback);
You can follow this example : Sample Here

How to create an action item with sub-items programmatically?

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.

Android Drawer Submenu with Edit Action and Title [Google Keep Example]

I want to have a group in my navigation drawer which has a title and next to the title there should be an edit button like in the new Google Keep App (see example below). Currently I can't find a sleek and working solution.
My menu layout at the moment:
<?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:title="Accounts"
app:actionLayout="#layout/edit_action"
app:showAsAction="always"
android:enabled="false">
<menu>
<group android:checkableBehavior="single">
<item android:title="Item 1" android:icon="#drawable/ic_outline_calendar_today"/>
<item android:title="Item 2" android:icon="#drawable/ic_outline_calendar_today"/>
</group>
<item android:title="Add new Item" android:icon="#drawable/ic_outline_add"/>
</menu>
</item>
<group>
<item android:title="Settings" android:icon="#drawable/ic_outline_settings"/>
<item android:title="Trash" android:icon="#drawable/ic_outline_delete"/>
<item android:title="Help \u0026 Feedback" android:icon="#drawable/ic_outline_feedback"/>
</group>
</menu>
Currently only the title is displayed in the submenu but the edit button is missing. But if you just create an item without a menu child, the edit button will be displayed but the item is now an item and not a title as before.
I would like to have the same result as in the picture. The padding as well as the text size and color should be the default values from Android.
My edit_action.xml file looks like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/actionButtonStyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true">
<TextView
android:text="Edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimary"
android:id="#+id/edit"/>
</RelativeLayout>
My menu looks like this at the moment:

Custom Layout menu item shows empty if showAsAction=never

I am trying to add SwitchCompat to Overflow menu using this code below:
main.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/menu_dummy_content"
android:title=""
android:actionViewClass="android.support.v7.widget.SwitchCompat"
app:showAsAction="never"
app:actionLayout="#layout/menu_item_switch"/>
<!-- TODO: Delete this after bug fix-->
<item
android:id="#+id/menu_dummy_content_2"
android:title="Second Title"
app:showAsAction="never"/>
</menu>
The layout for menu_item_switch is:
<android.support.v7.widget.SwitchCompat
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/menu_item_switch_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/menu_item_dummy_tasks"
android:layout_centerVertical="true"/>
If I do app:showAsAction="ifRoom" or anything, the switch appears perfectly fine and the listeners work fine. But as soon as I make the showAsAction as never. The switch view becomes white/empty but the onOptionsItemSelected says the clicked item is menu_dummy_content.
I also tried using only TextView in the custom layout, and that too comes as empty. Am I missing something?
Since you set the showAsAction attribute to never, then these menu item will never show as action layout.
I believe you need to add Title Text in here...
<item
android:id="#+id/menu_dummy_content"
android:title=""
android:actionViewClass="android.support.v7.widget.SwitchCompat"
app:showAsAction="never"
app:actionLayout="#layout/menu_item_switch"/>
like this...
android:title="First Title"

Categories

Resources