Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 614
Sahil Dave Avatar asked Nov 10 '22 14:11

Sahil Dave


1 Answers

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"
like image 119
iMDroid Avatar answered Nov 23 '22 01:11

iMDroid