Search code examples
javaandroid-studiolayoutmenudrawerlayout

Android menu Item can not be fit to its parent layout


when action_layout_avl layout is calling its width is fit the parent but when it is included in menu as actionLayout, the width just cover half of the parent page.

below is menu Item:

<?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" 
 xmlns:tools="http://schemas.android.com/tools"
  android:layout_gravity="end"
  android:gravity="end"
  android:layoutDirection="rtl">    
    <item
       android:id="@+id/map_services"
       android:title="title">
        <menu>
           <group>
               <item                                            
               android:id="@+id/switch_menu_item_poweroff"
               android:title=""
               app:actionLayout="@layout/action_layout_powwerroff
               app:showAsAction="always" />
                <item         
                android:id="@+id/switch_menu_item_avl"
                android:title=""
                app:actionLayout="@layout/action_layout_avl"
                app:showAsAction="always"/>
                <item
                android:id="@+id/menu_item_routing"
                android:icon="@drawable/route"
                android:title="@string/label_routing"
                app:showAsAction="ifRoom"/>
              </group>
            </menu>
         </item>
       </menu>

below is action_layout_avl layout

<?xml version="1.0" encoding="utf-8"?>
 <android.support.constraint.ConstraintLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal"
  android:textDirection="ltr"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:gravity="left">
 <TextView
    android:id="@+id/textavl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginStart="8dp"
    android:gravity="left"
    android:text="AVL"
    app:layout_constraintVertical_weight=".6"
    app:layout_constraintEnd_toEndOf="parent"/>
   <ImageButton
    android:id="@+id/buttonAvlFilter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@null"
    android:src="@drawable/ic_settings_black_24dp"
    app:layout_constraintLeft_toRightOf="@+id/switchAvlWS"
    app:layout_constraintRight_toLeftOf="@+id/textavl"
    app:layout_constraintTop_toTopOf="parent" />
   <Switch
    android:id="@+id/switchAvlWS"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOff="خاموش"
    android:textOn="روشن"
    app:layout_constraintStart_toStartOf="parent"/>
     </android.support.constraint.ConstraintLayout>

Normally the behavior of the layout should not change when it is calling inside menu item.what could be wrong in the code which cause this problem? Any help can be appreciated


Solution

  • Finally I solved this problem, this issue related to setting title for menu item even the title is empty. It by default allocates half space for the title and icon and another half space for action layout. So, I just remove title and set tools:ignore="MenuTitle".

            <group>
                <item
                    android:id="@+id/switch_menu_item_poweroff"
                    app:actionLayout="@layout/action_layout_powwerroff"
                    app:showAsAction="always"
                    tools:ignore="MenuTitle" />
                <item
                    android:id="@+id/switch_menu_item_avl"
                    app:actionLayout="@layout/action_layout_avl"
                    app:showAsAction="always"
                    tools:ignore="MenuTitle" />
                <item
                    android:id="@+id/menu_item_routing"
                    android:icon="@drawable/route"
                    android:title="@string/label_routing"
                    app:showAsAction="ifRoom"
    
                    />
            </group>