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 } });
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With