0

I have added a android.support.v7.widget.SwitchCompat switch on Toolbar. Here is my code:

SwitchCompat layout file toolbar_switch.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <android.support.v7.widget.SwitchCompat
        android:id="@+id/toolbar_switch"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text=""/>

</RelativeLayout>


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/action_clipboard_switch"
    android:title=""
    app:actionLayout="@layout/toolbar_switch"
    android:checkable="true"
    android:enabled="true"
    app:showAsAction="always"/>


This code is just showing me a switch on Toolbar. But when I click on switch nothing happens. I doesn't even toggle.
I've also added a Toast to onClickListener but it doesn't work. Help!!!

EDIT Neither Switch nor SwitchCompat Works!

zackygaurav
  • 4,369
  • 5
  • 26
  • 40

1 Answers1

0

I figured it on my own and I feel dumb now.

I had declared a TextView below the Toolbar and declared its layout_height and layout_width to match_parent.

    <TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textIsSelectable="true"
    android:layout_centerInParent="true"
    android:gravity="center"
    android:textSize="32sp"
    android:textStyle="bold"
    android:text="@string/text"/>

TextView was filling up the entire screen and due to which the Switches were not working.

zackygaurav
  • 4,369
  • 5
  • 26
  • 40