0

I've got a custom LinearLayout with a widget Switch and TextView child. I'd like to be able to click the area covered by the LinearLayout, so I set clickable=true to the LinearLayout, but Switch is not triggered.

I wanted to trigger on Switch when I click on the LinearLayout. I also tried with android:duplicateParentState="true" in Switch but did't worked.

Can I achieve this from XML?

 <LinearLayout
    android:background="@drawable/ripple"
    android:clickable="true"
    android:focusable="true"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Switch
        android:clickable="false"
        android:paddingTop="8dp"
        android:text="Visibility"
        android:textSize="16sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:clickable="false"
        android:layout_marginRight="58dp"
        android:maxLines="3"
        android:text="@string/visibility_desc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="58dp" />

</LinearLayout>
Victory
  • 1,184
  • 2
  • 11
  • 30

3 Answers3

1

Set Id of your linearlayout and Switch and when your perform click of Linearlayout you must want to perform checked of Switch.

linearLayout.setOnClickListener
{
   onClick()
   {
      Switch.setChecked(true);}
   }
}
niceumang
  • 1,347
  • 1
  • 8
  • 21
  • @VijayKumar You can't achieve it from XML because you should perform click of switch or linearlayout as per your functionality. – niceumang Jan 15 '20 at 10:48
0

You can try below code to obtain required output

<LinearLayout
    android:id="@+id/lnrLayout"
    android:background="@drawable/ripple"
    android:clickable="true"
    android:focusable="true"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Switch
        android:id="@+id/switch"
        android:clickable="false"
        android:paddingTop="8dp"
        android:text="Visibility"
        android:textSize="16sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:clickable="false"
        android:layout_marginRight="58dp"
        android:maxLines="3"
        android:text="@string/visibility_desc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="58dp" />

</LinearLayout>

Then set setOnClickListener of linearlayout:

 lnrLayout.setOnClickListener{
            switch.performClick()
        }

Or you can use switch with text.

 <androidx.appcompat.widget.SwitchCompat
                android:id="@+id/switch1"
                style="@style/tvStyle_poppinsMedium_Small_Black"
                android:text="@string/push_notifications"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
Nehal Godhasara
  • 787
  • 3
  • 7
0

you can try below code to achieve via xml -

<LinearLayout
android:background="@drawable/ripple"
android:clickable="true"
android:focusable="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<Switch
    android:paddingTop="8dp"
    android:text="Visibility"
    android:textSize="16sp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

<TextView
    android:clickable="false"
    android:layout_marginRight="58dp"
    android:maxLines="3"
    android:text="@string/visibility_desc"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="58dp" />

</LinearLayout>
Dipankar Baghel
  • 1,932
  • 2
  • 12
  • 24