android - Consume preference click event -
i facing problem while trying consume click event of preference. there's preference, , on clicking it, open sub screen different preference categories. want achieve if condition false, toast should popup, , sub screen shouldn't open. in case it's true, should work , open sub screen. this, tried following piece of code :
mpref = (preference) findpreference("abc"); mpref.setonpreferenceclicklistener(this); @override public boolean onpreferenceclick(preference preference) { // todo auto-generated method stub if (!condition) { toast.maketext(getactivity(), "error", toast.length_short).show(); return true; // consume click event. } return false; } the relevant part of xml follows:
<?xml version="1.0" encoding="utf-8"?> <preferencescreen android:key="abc" android:summary="@string/string_turn_on" android:textcolor="@android:color/white" android:title="@string/string_turn_on"> <preferencecategory android:key="asa" android:title="a" > <com.example.gaurav.custompref android:defaultvalue="0" android:key="st" android:dependency="e" android:showdefault="true" android:summary="summary" android:title="t" /> <com.example.gaurav.custompref android:defaultvalue="0" android:key="st" android:dependency="e" android:showdefault="true" android:summary="so" android:title="so" /> </preferencecategory> <preferencecategory android:title="other" > <checkboxpreference android:defaultvalue="false" android:key="e" android:title="eb" /> </preferencecategory> </preferencescreen> i have changed strings etc, don't think cause error, won't matter. it's preference key "abc" launches sub screen want prevent launching. regarding appreciated.
try adding line in oncreate method
addpreferencesfromresource(r.xml.preferences); edit:
@override public boolean onpreferenceclick(preference preference) { if (condition) { return true; // consume click event. } toast.maketext(getactivity(), "error", toast.length_short).show(); return false; }
Comments
Post a Comment