7

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 clicking on text title. As i click on text title, the switch is toggled. But i want to do switching only if i am clicking on toggle icon.

Is it possible? Or do i have to create two separate views for that: one TextView for title and one SwitchCompat without text?

enter image description here

User_1191
  • 981
  • 2
  • 8
  • 24
yital9
  • 6,544
  • 15
  • 41
  • 54

3 Answers3

0

I don't find an exact solution for this problem, but you can separate TextView and Swich like this:

<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 switch setOnCheckedChangeListener in your code.

Houssin Boulla
  • 2,687
  • 1
  • 16
  • 22
0

I hope this is not too late..

I wasn't able to create a separated textview because we have some accessibility restrictions but I came up with this solution:

I just created a custom component and overridden onTouchEvent:

override fun onTouchEvent(ev: MotionEvent): Boolean {
    if (ev.action == MotionEvent.ACTION_DOWN && thumbDrawable.bounds.contains(
            ev.x.toInt(),
            ev.y.toInt()
        )
    ) {
        toggle()
    }
    return true
}
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
-4

Add this:

android:clickable="true"
android:focusable="true"
Anirudh Sharma
  • 7,968
  • 13
  • 40
  • 42
MatejS
  • 19
  • 1
  • 1
  • 2