Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable swipe in switch compat? [duplicate]

I need to disable swipe in switch compat.I am using OnclickListener for toggling the switch.I have implemented some functionality for onClickListener.But when the user swipes the toggle he is not getting the implemented functionality.I need to disable swipe for switch compat.

Note: I am not using onCheckedlistener since I was getting some issues withonCheckedListener.

<android.support.v7.widget.SwitchCompat
    android:id="@+id/switchButton"
    android:layout_width="wrap_content"
    android:layout_centerInParent="true"
    android:checked="false"
    android:scrollbarAlwaysDrawVerticalTrack="true"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Switch example"
    />

SwitchCompat switchCompat;
switchCompat=(SwitchCompat)findViewById(R.id.switchButton);
     switchCompat.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getApplicationContext(), "toggled", Toast.LENGTH_SHORT).show(); //added
        }
    });
like image 396
winchester100 Avatar asked Feb 15 '18 06:02

winchester100


1 Answers

Try to prevent switch class from receiving swipe events by using the following code:

switchBtn.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return event.getActionMasked() == MotionEvent.ACTION_MOVE;
    }
});

this may prevent the switch from receiving the swipe

like image 63
Harsh Patel Avatar answered Sep 27 '22 02:09

Harsh Patel