Search code examples
javaandroidandroid-layoutandroid-actionbarandroid-toolbar

Adding text below/above a switch button in Android menu icon


I am trying to add a text below/above this switch item that is in my Toolbar to indicate functionality of the switch. I was wondering if there is a layout tag I could use to add a text below/above the button. Or is there some other method I have to use.

menu_main.xml

<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"
    tools:context=".MainActivity">

    <item
        android:id="@+id/edit"
        android:title="@string/app_name"
        app:actionViewClass="android.widget.Switch"
        android:icon="@drawable/ic_edit"
        app:showAsAction="always" android:visible="true"/>

Currently it looks like this:

Toolbar


Solution

  • You can create a custom menu layout that has a Switch and a TextView of its description

    layout/menu_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/menuRoot"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="8dp"
        android:gravity="center"
        android:orientation="vertical">
    
        <androidx.appcompat.widget.SwitchCompat
            android:id="@+id/menu_switch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
        <TextView
            android:id="@+id/switch_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="OFF"
            android:textColor="@color/white"
            android:textSize="12sp" />
    
    </LinearLayout>
    

    Then utilize the app:actionLayout attribute of the menu item to attach the custom menu

    menu/my_menu.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/switchItem"
            android:title="switch"
            app:actionLayout="@layout/menu_layout"
            app:showAsAction="always" />
    
    </menu>
    

    And you can detect the switch click callbacks and change the description accordingly as below:

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.my_menu, menu);
    
            MenuItem item = menu.findItem(R.id.switchItem);
            LinearLayout root = item.getActionView().findViewById(R.id.menuRoot);
            SwitchCompat menuSwitch = root.findViewById(R.id.menu_switch);
            TextView switchText = root.findViewById(R.id.switch_text);
            menuSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    switchText.setText(isChecked? "ON" : "OFF");
                }
            });
            return true;
        }
    

    A preview