Example usage for android.preference ListPreference setOnPreferenceChangeListener

List of usage examples for android.preference ListPreference setOnPreferenceChangeListener

Introduction

In this page you can find the example usage for android.preference ListPreference setOnPreferenceChangeListener.

Prototype

public void setOnPreferenceChangeListener(OnPreferenceChangeListener onPreferenceChangeListener) 

Source Link

Document

Sets the callback to be invoked when this Preference is changed by the user (but before the internal state has been updated).

Usage

From source file:bander.notepad.PrefsFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.preferences);

    ListPreference sortOrderPreference = (ListPreference) findPreference(KEY_SORTORDER);
    sortOrderPreference.setOnPreferenceChangeListener(this);
    setSortOrderSummary(sortOrderPreference);
    setSortAscendingEnabled(!sortOrderPreference.getValue().equals("0"));

    ListPreference textSizePreference = (ListPreference) findPreference(KEY_TEXTSIZE);
    textSizePreference.setOnPreferenceChangeListener(this);
    setTextSizeSummary(textSizePreference);

    ListPreference themePreference = (ListPreference) findPreference(KEY_THEME_TYPE);
    themePreference.setOnPreferenceChangeListener(this);

    ListPreference colorSchemePreference = (ListPreference) findPreference(KEY_COLOR);
    colorSchemePreference.setOnPreferenceChangeListener(this);

    Preference aboutPreference = findPreference(KEY_ABOUT);
    aboutPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

        @Override/*from   w w  w . ja v  a 2s . c  om*/
        public boolean onPreferenceClick(Preference preference) {
            FragmentTransaction ft = getChildFragmentManager().beginTransaction();
            About.newInstance().show(ft, "dialog");
            return false;
        }
    });

}

From source file:org.dmfs.webcal.fragments.PreferencesFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.webcal_preference);
    ListPreference locales = (ListPreference) findPreference("content_location");

    locales.setOnPreferenceChangeListener(PrefUpdater);

    try {//from w w w . j a v  a2 s  . co  m
        getCountries();

        locales.setEntries(mCountryNames);
        locales.setEntryValues(mCountryCodes);
    } catch (Exception e) {
        Log.e(TAG, "could not load country list", e);
    }

    int index = locales.findIndexOfValue(locales.getValue());
    if (index >= 0) {
        locales.setSummary(locales.getEntries()[index]);
    } else {
        locales.setSummary(locales.getEntries()[0]);
        locales.setValueIndex(0);
    }
}

From source file:com.bellman.bible.android.view.activity.settings.SettingsActivity.java

private void addScreenTimeoutSettings() {
    ListPreference timeoutPref = (ListPreference) getPreferenceScreen()
            .findPreference(ScreenTimeoutSettings.SCREEN_TIMEOUT_PREF);

    timeoutPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

        @Override/*from   w w  w  . j  a  v  a2s  .  com*/
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            // update screen timeout
            try {
                if (StringUtils.isNotEmpty((String) newValue)) {
                    ScreenTimeoutSettings.setScreenTimeout(Integer.parseInt((String) newValue));
                }
            } catch (Exception e) {
                Log.e(TAG, "Error updating timeout set in preference screen", e);
            }

            return true;
        }
    });

    ScreenTimeoutSettings screenTimeoutSettings = new ScreenTimeoutSettings();
    timeoutPref.setEntries(screenTimeoutSettings.getPreferenceEntries());
    timeoutPref.setEntryValues(screenTimeoutSettings.getPreferenceEntryValues());
}

From source file:fr.free.nrw.commons.settings.SettingsFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    AndroidInjection.inject(this);
    super.onCreate(savedInstanceState);

    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preferences);

    // Update spinner to show selected value as summary
    ListPreference licensePreference = (ListPreference) findPreference(Prefs.DEFAULT_LICENSE);
    licensePreference.setSummary(getString(Utils.licenseNameFor(licensePreference.getValue())));

    // Keep summary updated when changing value
    licensePreference.setOnPreferenceChangeListener((preference, newValue) -> {
        preference.setSummary(getString(Utils.licenseNameFor((String) newValue)));
        return true;
    });/*  w  w w .  j  a  v a2s .  c o  m*/

    CheckBoxPreference themePreference = (CheckBoxPreference) findPreference("theme");
    themePreference.setOnPreferenceChangeListener((preference, newValue) -> {
        getActivity().recreate();
        return true;
    });

    final EditTextPreference uploadLimit = (EditTextPreference) findPreference("uploads");
    int uploads = prefs.getInt(Prefs.UPLOADS_SHOWING, 100);
    uploadLimit.setText(uploads + "");
    uploadLimit.setSummary(uploads + "");
    uploadLimit.setOnPreferenceChangeListener((preference, newValue) -> {
        int value = Integer.parseInt(newValue.toString());
        final SharedPreferences.Editor editor = prefs.edit();
        if (value > 500) {
            new AlertDialog.Builder(getActivity()).setTitle(R.string.maximum_limit)
                    .setMessage(R.string.maximum_limit_alert)
                    .setPositiveButton(android.R.string.yes, (dialog, which) -> {
                    }).setIcon(android.R.drawable.ic_dialog_alert).show();
            editor.putInt(Prefs.UPLOADS_SHOWING, 500);
            editor.putBoolean(Prefs.IS_CONTRIBUTION_COUNT_CHANGED, true);
            uploadLimit.setSummary(500 + "");
            uploadLimit.setText(500 + "");
        } else {
            editor.putInt(Prefs.UPLOADS_SHOWING, Integer.parseInt(newValue.toString()));
            editor.putBoolean(Prefs.IS_CONTRIBUTION_COUNT_CHANGED, true);
            uploadLimit.setSummary(newValue.toString());
        }
        editor.apply();
        return true;
    });

    Preference sendLogsPreference = findPreference("sendLogFile");
    sendLogsPreference.setOnPreferenceClickListener(preference -> {
        //first we need to check if we have the necessary permissions
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(getActivity(),
                    Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                sendAppLogsViaEmail();
            } else {
                //first get the necessary permission
                requestPermissions(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },
                        REQUEST_CODE_WRITE_EXTERNAL_STORAGE);
            }
        } else {
            sendAppLogsViaEmail();
        }

        return true;
    });
}

From source file:fi.iki.murgo.irssinotifier.SettingsActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.d(TAG, "Opened settings");
    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.preference_screen);

    final Context ctx = this;

    final CheckBoxPreference enabled = (CheckBoxPreference) findPreference("NotificationsEnabled");
    enabled.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            final boolean checked = (Boolean) newValue;
            String s = "Disabling notifications...";
            if (checked) {
                s = "Enabling notifications...";
            }//from   ww  w.  jav  a 2 s . com

            SettingsSendingTask task = new SettingsSendingTask(SettingsActivity.this, "Sending settings", s);
            task.setCallback(new Callback<ServerResponse>() {
                @Override
                public void doStuff(ServerResponse result) {
                    if (result.getException() != null) {
                        if (result.getException() instanceof IOException) {
                            MessageBox.Show(ctx, "Network error",
                                    "Ensure your internet connection works and try again.", null);
                        } else if (result.getException() instanceof AuthenticationException) {
                            MessageBox.Show(ctx, "Authentication error", "Unable to authenticate to server.",
                                    null);
                        } else if (result.getException() instanceof ServerException) {
                            MessageBox.Show(ctx, "Server error",
                                    "Mystical server error, check if updates are available", null);
                        } else {
                            MessageBox.Show(ctx, null,
                                    "Unable to send settings to the server! Please try again later!", null);
                        }
                        enabled.setChecked(!checked);
                    }
                }
            });

            task.execute();

            return true;
        }
    });

    Preference aboutPref = findPreference("about");
    aboutPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
        public boolean onPreferenceClick(Preference preference) {
            Intent i = new Intent(ctx, AboutActivity.class);
            startActivity(i);

            finish();
            return true;
        }
    });

    Preference channelsPref = findPreference("channels");
    channelsPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
        public boolean onPreferenceClick(Preference preference) {
            Intent i = new Intent(ctx, ChannelSettingsActivity.class);
            startActivity(i);
            return true;
        }
    });

    ListPreference mode = (ListPreference) findPreference("notificationModeString");
    mode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            NotificationMode m = NotificationMode.PerChannel;
            String v = (String) newValue;
            if (v.equals(ctx.getResources().getStringArray(R.array.notification_modes)[0]))
                m = NotificationMode.Single;
            if (v.equals(ctx.getResources().getStringArray(R.array.notification_modes)[1]))
                m = NotificationMode.PerChannel;
            if (v.equals(ctx.getResources().getStringArray(R.array.notification_modes)[2]))
                m = NotificationMode.PerMessage;

            Preferences p = new Preferences(ctx);
            p.setNotificationMode(m);
            return true;
        }
    });

    Preference initialSettingsPref = findPreference("redoInitialSettings");
    initialSettingsPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
        public boolean onPreferenceClick(Preference preference) {
            Preferences prefs = new Preferences(ctx);
            prefs.setAuthToken(null);
            prefs.setAccountName(null);
            prefs.setGcmRegistrationId(null);
            prefs.setLicenseCount(0);

            IrssiNotifierActivity.refreshIsNeeded();
            finish();
            return true;
        }
    });

    Preference disableThemePref = findPreference("ThemeDisabled");
    disableThemePref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
        public boolean onPreferenceClick(Preference preference) {
            IrssiNotifierActivity.refreshIsNeeded();
            return true;
        }
    });

    handleColorPicker();

    handleIcb();

    if (!LicenseHelper.isPlusVersion(this)) {
        CheckBoxPreference usePullMechanismPref = (CheckBoxPreference) findPreference("UsePullMechanism");
        usePullMechanismPref.setSummary(usePullMechanismPref.getSummary() + ". Only in Plus version.");
        usePullMechanismPref.setEnabled(false);
        usePullMechanismPref.setChecked(false);
    }
}

From source file:de.hero.vertretungsplan.PrefsActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);

    mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

    ListPreference listPref = (ListPreference) findPreference("prefs_benachrichtigungsintervall");
    listPref.setSummary(/*  ww  w .ja  v  a2s. c  o m*/
            getSummaryInterval(mySharedPreferences.getString("prefs_benachrichtigungsintervall", "1")));
    listPref.setOnPreferenceChangeListener(new ListPreference.OnPreferenceChangeListener() {
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            preference.setSummary(getSummaryInterval(newValue.toString()));

            MainActivity.setNewAlarm(getBaseContext(), true, newValue.toString());
            return true;
        }

    });

    Preference klasse = findPreference("prefs_klasse");
    klasse.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

        public boolean onPreferenceClick(Preference preference) {
            showDialog(R.string.dialog_klasse);
            return true;
        }
    });

    CheckBoxPreference ckBxPref = (CheckBoxPreference) findPreference("prefs_benachrichtigungen");
    ckBxPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

        public boolean onPreferenceChange(Preference preference, Object newValue) {
            if (preference.getKey().equals("prefs_benachrichtigungen")) {
                MainActivity.setNewAlarm(getBaseContext(), !((CheckBoxPreference) preference).isChecked(),
                        mySharedPreferences);
            }
            return true;
        }
    });

    CheckBoxPreference ckBxPrefAppUpdate = (CheckBoxPreference) findPreference("prefs_check_for_app_updates");
    ckBxPrefAppUpdate.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

        public boolean onPreferenceChange(Preference preference, Object newValue) {
            if (preference.getKey().equals("prefs_check_for_app_updates")) {
                Intent i = new Intent(getBaseContext(), CheckForAppUpdate.class);
                i.putExtra("setTimer", !((CheckBoxPreference) preference).isChecked());
                if (!((CheckBoxPreference) preference).isChecked()) {
                    i.putExtra("checkNow", true);
                }
                getBaseContext().startService(i);
            }
            return true;
        }
    });

    Preference ueber = findPreference("ueber");
    ueber.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

        public boolean onPreferenceClick(Preference preference) {
            Dialog ueberDialog = new Dialog(PrefsActivity.this);

            ueberDialog.setContentView(R.layout.ueber_dialog_layout);
            ueberDialog.setTitle(getString(R.string.ueber));
            ueberDialog.setCancelable(true);
            ueberDialog.setCanceledOnTouchOutside(true);

            TextView dialogText = (TextView) ueberDialog.findViewById(R.id.text_in_dialog);
            dialogText.setTextColor(Color.BLACK);
            dialogText.setAutoLinkMask(Linkify.ALL);

            dialogText.setText(String.format(getString(R.string.ueberTextFormated),
                    getString(R.string.version_nr), getString(R.string.emailAdresseEntwickler),
                    getString(R.string.webAdresseAppDownload), getString(R.string.GNU_GPLwebadresse)));
            ueberDialog.show();
            return true;
        }
    });

    CheckBoxPreference ckBxPrefDebugging = (CheckBoxPreference) findPreference("prefs_debug");
    ckBxPrefDebugging.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

        public boolean onPreferenceChange(Preference preference, Object newValue) {
            if (preference.getKey().equals("prefs_debug")) {
                Log.d("PrefsActivity", "new");
                if (!((CheckBoxPreference) preference).isChecked()) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(preference.getContext());

                    builder.setMessage(getString(R.string.debugText)).setTitle(getString(R.string.debugTitel));

                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
            }
            return true;
        }
    });
}

From source file:com.jparkie.aizoban.presenters.SettingsPresenterImpl.java

@Override
public void initializeDownloadDirectory() {
    ListPreference downloadPreference = mSettingsMapper.getDownloadStoragePreference();
    if (downloadPreference != null) {
        String[] downloadDirectories = DiskUtils.getStorageDirectories();

        downloadPreference.setEntries(downloadDirectories);
        downloadPreference.setEntryValues(downloadDirectories);

        downloadPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override//from   w ww  .j a  v  a2  s . c  om
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                String downloadDirectory = (String) newValue;
                if (downloadDirectory != null) {
                    File actualDirectory = new File(downloadDirectory);

                    if (!actualDirectory.equals(AizobanApplication.getInstance().getFilesDir())) {
                        boolean isWritable = actualDirectory.mkdirs();

                        try {
                            File tempFile = File.createTempFile("tempTestDirectory", "0", actualDirectory);
                            tempFile.delete();

                            isWritable = true;
                        } catch (IOException e) {
                            isWritable = false;
                        }

                        if (!isWritable) {
                            mSettingsView.toastExternalStorageError();

                            return false;
                        }
                    }
                }

                return true;
            }
        });
    }
}

From source file:com.airflo.preferences.ListPreferenceActivity.java

/**
 * Add one slider preference to the category.
 * @param cat//from   w  ww  .j av a 2s . c  om
 * @param key
 * @param title
 */
private void addOneListItem(PreferenceCategory cat, String key, String title) {
    final ListPreference listPref = new ListPreference(this);
    listPref.setKey(key);
    listPref.setEntries(FlightData.identis.getPrefListChars(true));
    listPref.setEntryValues(FlightData.identis.getPrefListChars(false));
    listPref.setDialogTitle(title);
    listPref.setSummary(
            FlightData.identis.getIdenti(root.getSharedPreferences().getString(key, "empty")).getStringRep());
    listPref.setTitle(title);
    cat.addPreference(listPref);
    listPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            listPref.setSummary(FlightData.identis.getIdenti((String) newValue).getStringRep());
            return true;
        }
    });
}

From source file:fr.pasteque.client.Configure.java

@Override
public void onCreate(Bundle state) {
    super.onCreate(state);
    // Set default values
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    if (!prefs.contains("payleven")) {
        SharedPreferences.Editor edit = prefs.edit();
        edit.putBoolean("payleven", Compat.hasPaylevenApp(this));
        edit.apply();//from  w w w.j  a  va2 s .com
    }
    // Load preferences
    this.addPreferencesFromResource(R.xml.configure);
    this.printerDrivers = (ListPreference) this.findPreference("printer_driver");
    this.printerModels = (ListPreference) this.findPreference("printer_model");
    this.printerDrivers.setOnPreferenceChangeListener(this);
    this.updatePrinterPrefs(null);

    ListPreference card_processor = (ListPreference) this.findPreference("card_processor");
    card_processor.setOnPreferenceChangeListener(this);
    this.updateCardProcessorPreferences(null);
    if (this.comesFromError()) {
        this.showError(this.getError());
    }
    if (Configure.isDemo(this)) {
        findPreference("user").setEnabled(false);
        findPreference("password").setEnabled(false);
    }
}

From source file:com.airflo.preferences.ListPreferenceActivity.java

@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
@Override/*from   w  ww  . ja  v a 2s.c o m*/
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    root = getPreferenceManager().createPreferenceScreen(this);

    //Array of sorting preference keys to be included. 
    //SortType must be specified in bookitems.xml!
    String[] sortValues = new String[] { "number", "date;starttime", "site;date;starttime", "duration",
            "maxheight", "maxvario", "starttype;date;starttime" };
    //Generate according Entry Strings.
    String[] sortEntries = new String[sortValues.length];
    for (int i = 0; i < sortEntries.length; i++)
        sortEntries[i] = sortKeyToString(sortValues[i]);

    //Make sorting category
    PreferenceCategory cat = new PreferenceCategory(this);
    cat.setTitle(R.string.list_pref_cat_sort_title);
    root.addPreference(cat);
    final ListPreference listPref = new ListPreference(this);
    listPref.setKey("list_pref_sort");
    listPref.setEntries(sortEntries);
    listPref.setEntryValues(sortValues);
    listPref.setDialogTitle(R.string.list_pref_sort_criterium);
    listPref.setSummary(sortKeyToString(root.getSharedPreferences().getString("list_pref_sort", "number")));
    listPref.setTitle(R.string.list_pref_sort_criterium);
    cat.addPreference(listPref);
    listPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            listPref.setSummary(sortKeyToString((String) newValue));
            return true;
        }
    });
    final ListPreference orderPref = new ListPreference(this);
    orderPref.setKey("list_pref_order");
    orderPref.setEntries(new String[] { OnlyContext.rString("list_pref_sort_ascending"),
            OnlyContext.rString("list_pref_sort_decending") });
    orderPref.setEntryValues(new String[] { "list_pref_sort_ascending", "list_pref_sort_decending" });
    orderPref.setDialogTitle(R.string.list_pref_sort_sequence);
    orderPref.setSummary(OnlyContext
            .rString(root.getSharedPreferences().getString("list_pref_order", "list_pref_sort_decending")));
    orderPref.setTitle(R.string.list_pref_sort_sequence);
    cat.addPreference(orderPref);
    orderPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            orderPref.setSummary(OnlyContext.rString((String) newValue));
            return true;
        }
    });

    //Make item categories
    addManyListItems(R.string.list_pref_cat1_title, "listhead", 3);
    addManyListItems(R.string.list_pref_cat2_title, "listsub", 3);

    this.setPreferenceScreen(root);
}