Android switchcompat turns off when you click on text

I have a SwitchCompat :

 <android.support.v7.widget.SwitchCompat android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Some title"/> 

and I want to prevent the text header from clicking. When I click on the text name, the switch toggles. But I want to switch only if I click on the switch icon.

Is it possible? Or do I need to create two separate views for this: one TextView for the title and one SwitchCompat without text?

enter image description here

+5
source share
2 answers

I have not found an exact solution to this problem, but you can separate TextView and Swich as follows:

 <RelativeLayout android:layout_width="wrap_content" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="YOUR SWITCH TEXT HERE"/> <Switch android:id="@+id/switch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" /> </RelativeLayout> 

And use the setOnCheckedChangeListener switch in your code.

0
source

Add this:

 android:clickable="true" android:focusable="true" 
-4
source

Source: https://habr.com/ru/post/1237825/


All Articles