Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Checkbox preference
    text
    copied!<p>I cannot find any tutorials on checkbox preference. I can use a listpreference, but I can't use checkbox preference. For now, I want that if user sets on the checbox, a toast msg says "true" and if he sets it off, the toast msg says "false". So far I have this:</p> <p>preferences.xml:</p> <pre><code> &lt;CheckBoxPreference android:title="Show Call UI" android:defaultValue="true" android:summary="Show Call Interface when clicking call button" android:key="checkboxPref" /&gt; </code></pre> <p>EditPreferences.java:</p> <pre><code>public class EditPreferences extends PreferenceActivity { String listPreference; boolean checkboxPreference; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); } public void onStart(Intent intent, int startId) { getPrefs(); } private void getPrefs() { SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences(getBaseContext()); listPreference = prefs.getString("listPref", "nr1"); checkboxPreference = prefs.getBoolean("checkboxPref", true); } } </code></pre> <p><strong>Edit: Solution thanks to David Caunt:</strong></p> <pre><code>checkboxPreference. setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { public boolean onPreferenceChange(Preference preference, Object newValue) { if (newValue.toString().equals("true")) { Toast.makeText(getApplicationContext(), "CB: " + "true", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), "CB: " + "false", Toast.LENGTH_SHORT).show(); } return true; } }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload