android - Can't toggle switch in Custom SwitchPreference -
i try custom switch preference in adroid. here code
switch_widget.xml <switch xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/customswitch" android:layout_width="match_parent" android:layout_height="wrap_content" android:focusable="false" android:clickable="false" /> preference xml file : <com.tnavitech.utils.customswitch android:title="hide app icon" android:key="hideicon" android:defaultvalue="false" android:disabledependentsstate="true" android:widgetlayout="@layout/switch_widget" />
then extend "switchpreference", , in "onbindview" class, , handle switch states in onpreferencechangelistener :
public class customswitch extends switchpreference { switch swit; public customcheckbox(context context, attributeset attrs) { super(context, attrs); } @override protected void onbindview(final view rootview) { viewgroup viewgroup= (viewgroup)rootview; super.onbindview(rootview); if(swit==null){ swit = (switch) rootview.findviewbyid(r.id.customswitch); } this.setonpreferencechangelistener(new onpreferencechangelistener() { @override public boolean onpreferencechange(preference arg0, object arg1) { if (arg1 instanceof boolean) { boolean enabled = (boolean) arg1; if(enabled){ swit.setchecked(true); /// cant toggle switch here toast.maketext(getcontext(), "on", toast.length_short).show(); } else { toast.maketext(getcontext(), "off", toast.length_short).show(); swit.setselected(false); ///cant toggle switch here } } return true; } }); }
in setonpreferencechangelistener cant toggle swith on or off. how solve this? thanks!
i'm working on app running on device api 15 had fix issue. solution have 2 different layouts switchpreference instead of 1 handles switch toggling. switch needs clickable = false otherwise switchpreference won't handle clicks.
first 'on state' switch layout, button uses drawable represents on button:
<?xml version="1.0" encoding="utf-8"?> <switch xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/custom_switch_item" android:layout_width="48dp" android:layout_height="wrap_content" android:textcolor="@android:color/white" android:textisselectable="false" android:textsize="0sp" android:button="@drawable/ico_toggle_on" //here difference between these layouts android:thumb="@android:color/transparent" android:track="@android:color/transparent" android:clickable="false" android:focusable="false" android:gravity="center_vertical" />
second layout off state:
<?xml version="1.0" encoding="utf-8"?> <switch xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/custom_switch_item" android:layout_width="48dp" android:layout_height="wrap_content" android:textcolor="@android:color/white" android:textisselectable="false" android:textsize="0sp" android:button="@drawable/ico_toggle_off" //here difference between these layouts android:thumb="@android:color/transparent" android:track="@android:color/transparent" android:clickable="false" android:focusable="false" android:gravity="center_vertical" />
and in activity or fragment set on switchpreference onpreferencechangelistener switch these 2 layouts this:
final switchpreference expertsettingspref = (switchpreference) findpreference("expert_settings"); if (expertsettingspref != null) { expertsettingspref.setonpreferencechangelistener(new preference.onpreferencechangelistener() { @override public boolean onpreferencechange(preference preference, object newvalue) { preference.setwidgetlayoutresource((boolean)newvalue ? r.layout.custom_switch_preference_on : r.layout.custom_switch_preference_off); return true; } }); }
Comments
Post a Comment