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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With