Issue
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 } });
Solution
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
Answered By - Harsh Patel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.