Sunday 15 February 2015

android - ListPreference onClickListener takes twice to execute -



android - ListPreference onClickListener takes twice to execute -

so i'm having problem listpreference takes 2 clicks upon it, what's in onclick listener. here's code

@override public void onsharedpreferencechanged(sharedpreferences sharedpreferences, string key) { final listpreference preflistthemes = (listpreference) findpreference("prefmaterialthemes"); preflistthemes.setonpreferenceclicklistener(new preference.onpreferenceclicklistener() { @override public boolean onpreferenceclick(preference preference) { toast.maketext(getapplicationcontext(), "test", toast.length_short).show(); system.out.println("test prntln"); // restart activity apply theme intent intent = new intent(getapplicationcontext(), mainactivity.class); intent.addflags(intent.flag_activity_new_task | intentcompat.flag_activity_clear_task); startactivity(intent); homecoming false; } });

as can see i'm trying simple restart of app apply theme. @ first thought might intent code causing problem, when have toast in onclick listener still have click list preference, select option, click preference again, , select alternative 1 time more anything. if help great. give thanks you!

settingsactivity gets preferences preferences.xml

import android.content.context; import android.content.intent; import android.content.sharedpreferences; import android.graphics.color; import android.os.bundle; import android.preference.checkboxpreference; import android.preference.edittextpreference; import android.preference.listpreference; import android.preference.preference; import android.preference.preferenceactivity; import android.preference.preferencemanager; import android.support.v4.content.intentcompat; import android.widget.toast; import com.iliakplv.notes.r; import com.iliakplv.notes.gui.main.launcheractivity; import com.iliakplv.notes.gui.main.mainactivity; import java.util.list; public class settingsactivity extends preferenceactivity implements sharedpreferences.onsharedpreferencechangelistener { private static final int preferences = r.xml.preferences; private final static string prefs_list_of_themes = "prefmaterialthemes"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //noinspection deprecation addpreferencesfromresource(preferences); preferencemanager.setdefaultvalues(this, preferences, false); // these 3 lines required onsharedpreferencechanged method context context = getapplicationcontext(); sharedpreferences settings = preferencemanager.getdefaultsharedpreferences(context); settings.registeronsharedpreferencechangelistener(this); // end } @override public void onsharedpreferencechanged(sharedpreferences sharedpreferences, string key) { final checkboxpreference prefdarkthemecb = (checkboxpreference) findpreference("prefdarktheme"); prefdarkthemecb.setonpreferenceclicklistener(new preference.onpreferenceclicklistener() { @override public boolean onpreferenceclick(preference preference) { if (prefdarkthemecb.ischecked()) { // restart activity apply theme intent intent = new intent(getapplicationcontext(), mainactivity.class); intent.addflags(intent.flag_activity_new_task | intentcompat.flag_activity_clear_task); startactivity(intent); } else if (prefdarkthemecb.ischecked() == false) { // restart activity apply default theme intent intent = new intent(getapplicationcontext(), mainactivity.class); intent.addflags(intent.flag_activity_new_task | intentcompat.flag_activity_clear_task); startactivity(intent); } homecoming false; } }); final listpreference preflistthemes = (listpreference) findpreference("prefmaterialthemes"); preflistthemes.setonpreferencechangelistener(new preference.onpreferencechangelistener() { public boolean onpreferencechange(preference preference, object newvalue) { toast.maketext(getapplicationcontext(), "toast", toast.length_short).show(); system.out.println("toasted"); // restart activity apply theme intent intent = new intent(getapplicationcontext(), mainactivity.class); intent.addflags(intent.flag_activity_new_task | intentcompat.flag_activity_clear_task); startactivity(intent); homecoming false; } }); final checkboxpreference prefrequirepasswordcb = (checkboxpreference) findpreference("prefenablepasswordlock"); prefrequirepasswordcb.setonpreferenceclicklistener(new preference.onpreferenceclicklistener() { @override public boolean onpreferenceclick(preference preference) { if (prefrequirepasswordcb.ischecked()) { edittextpreference prefuserspass = (edittextpreference) findpreference("prefpassword"); // check see if users password empty before allowing them check require password box if (prefuserspass.gettext().tostring().isempty()) { prefrequirepasswordcb.setchecked(false); toast.maketext(getapplicationcontext(), r.string.password_required, toast.length_short).show(); //system.out.println("empty password"); } else if (prefuserspass.gettext().tostring().trim().length() == 0) { // above line checks see if user's pass spaces :p prefrequirepasswordcb.setchecked(false); toast.maketext(getapplicationcontext(), r.string.password_required, toast.length_short).show(); //system.out.println("trimmed password because used spaces , still empty"); } else { toast.maketext(getapplicationcontext(), r.string.password_has_been_set, toast.length_short).show(); //system.out.println("kapow! have password , its: " + prefuserspass.gettext().tostring()); } } homecoming false; } }); } }

preferences.xml

<?xml version="1.0" encoding="utf-8"?> <preferencescreen xmlns:android="http://schemas.android.com/apk/res/android"> <preferencecategory android:title="@string/security_pref_category"> <checkboxpreference android:defaultvalue="false" android:key="prefenablepasswordlock" android:summary="@string/pref_require_password_for_app_description" android:title="@string/pref_require_password_for_app" /> <edittextpreference android:defaultvalue="" android:key="prefpassword" android:summary="@string/pref_password_description" android:title="@string/pref_password_title" /> </preferencecategory> <preferencecategory android:title="@string/look_and_feel_pref_category"> <checkboxpreference android:defaultvalue="false" android:key="prefdarktheme" android:summary="@string/pref_dark_theme_description" android:title="@string/pref_dark_theme" /> <listpreference android:defaultvalue="1" android:entries="@array/pref_app_themes" android:entryvalues="@array/pref_app_theme_values" android:key="prefmaterialthemes" android:summary="@string/pref_change_theme_description" android:title="@string/pref_change_theme" /> </preferencecategory> <preferencecategory android:title="@string/misc_pref_category"> <checkboxpreference android:defaultvalue="true" android:key="linkify_note_text" android:summary="@string/settings_linkify_subtitle" android:title="@string/settings_linkify_title" /> <preference android:key="contactdevkey" android:summary="@string/pref_contact_developer_description" android:title="@string/pref_contact_developer"> <intent android:action="android.intent.action.view" android:data="mailto:support@oxpheen.com?subject=support *notes"> <extra android:name="android.intent.extra.text" android:value="" /> <!-- value whats in body, blank --> </intent> </preference> </preferencecategory> </preferencescreen>

arrays.xml (used in preference.xml file entries list preference)

<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="pref_app_themes"> <item name="1">default</item> <item name="2">dark</item> <item name="3">amoled</item> <item name="4">red</item> <item name="5">pink</item> <item name="6">purple</item> <item name="7">deep purple</item> <item name="8">blue</item> <item name="9">light blue</item> <item name="10">cyan</item> <item name="11">teal</item> <item name="12">green</item> <item name="13">light green</item> <item name="14">lime</item> <item name="15">yellow</item> <item name="16">amber</item> <item name="17">orange</item> <item name="18">deep orange</item> <item name="19">brown</item> <item name="20">grey</item> <item name="21">blue grey</item> </string-array> <string-array name="pref_app_theme_values"> <item name="1">1</item> <item name="2">2</item> <item name="3">3</item> <item name="4">4</item> <item name="5">5</item> <item name="6">6</item> <item name="7">7</item> <item name="8">8</item> <item name="9">9</item> <item name="10">10</item> <item name="11">11</item> <item name="12">12</item> <item name="13">13</item> <item name="14">14</item> <item name="15">15</item> <item name="16">16</item> <item name="17">17</item> <item name="18">18</item> <item name="19">19</item> <item name="20">20</item> <item name="21">21</item> </string-array> </resources>

onsharedpreferencechanged called 1 time preference changed. when happens set onpreferenceclicklistener triggered 1 time alter value again. that's reason code executed when changing theme value twice.

put oncreate instead:

final listpreference preflistthemes = (listpreference) findpreference("prefmaterialthemes"); preflistthemes.setonpreferencechangelistener(new preference.onpreferencechangelistener() { public boolean onpreferencechange(preference preference, object newvalue) { toast.maketext(getapplicationcontext(), "toast", toast.length_short).show(); system.out.println("toasted"); // restart activity apply theme intent intent = new intent(getapplicationcontext(), mainactivity.class); intent.addflags(intent.flag_activity_new_task | intentcompat.flag_activity_clear_task); startactivity(intent); homecoming true; } });

android onclicklistener preference

No comments:

Post a Comment