androidsettingscheckboxpreference

Implementing Settings page in Android Application


I am a new developer, and i'm trying to implement a Settings page in Android, but with not much luck.

I am using CheckBoxPreference, and i'm trying to make some SharedPreferences change when i check/uncheck a CheckBoxPreference.

Here is my code:

package com.entu.bocterapp;

//imports

public class Settings extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {

    CheckBoxPreference pushNotificationSetting;
    CheckBoxPreference locationSupportSetting;


    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings_design);
        initializeActionBar();
   }

    public void initializeActionBar(){
        ActionBar actionBar = getActionBar();
        actionBar.setBackgroundDrawable(new ColorDrawable(0xff50aaf1));

        getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        getActionBar().setCustomView(R.layout.action_bar_center_image);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

        if (key.matches("PUSH_NOTIFICATION_PREFERENCE")) {
            if (getPushNotificationsPreference().equals("true")) {
                setPushNotificationsPreference("false");
            } else {
                if (getPushNotificationsPreference().equals("false")) {
                    setPushNotificationsPreference("true");
                }
            }
           Toast.makeText(this, getPushNotificationsPreference(), Toast.LENGTH_SHORT).show();
        }

        if (key.matches("LOCATION_SUPPORT_PREFERENCE")) {
            if (getLocationSupportPreference().equals("true")) {
                setLocationSupportPreference("false");
            } else {
                if (getLocationSupportPreference().equals("false")) {
                    setLocationSupportPreference("true");
                }
            }
        }
        Toast.makeText(this, getLocationSupportPreference(), Toast.LENGTH_SHORT).show();
    }

    public void setPushNotificationsPreference(String value) {
        SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString(getString(R.string.push_notification_preference), value);
        editor.commit();
    }

    public String getPushNotificationsPreference() {
        SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
        return sharedPref.getString(getString(R.string.push_notification_preference), "true");

    }

    public void setLocationSupportPreference(String value) {
        SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString(getString(R.string.location_support_preference), value);
        editor.commit();
    }

    public String getLocationSupportPreference() {
        SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
        return sharedPref.getString(getString(R.string.location_support_preference), "true");

    }
}

Here is my XML:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory
        android:title="BocterApp Settings">

        <CheckBoxPreference
            android:key="PUSH_NOTIFICATION_PREFERENCE"
            android:title="Push Notifications"
            android:summary="Uncheck this if you don't want to receive push notifications for alerts in your area (2km radius)."
            android:defaultValue="true"/>


        <CheckBoxPreference
            android:key="LOCATION_SUPPORT_PREFERENCE"
            android:title="Location Support"
            android:summary="Filters locations that are more than 15 km away. Uncheck this if you want to see all alerts."
            android:defaultValue="true"/>


    </PreferenceCategory>


</PreferenceScreen>

Can you tell me what am I doing wrong/how to implement it correctly?


Solution

  • I think, you want to change the CheckBoxPreference summary. You can use android:summaryOn to set the summary while its value is true. And use android:summaryOff to set the summary while its value is false. It will looks like this:

    <CheckBoxPreference
        android:key="LOCATION_SUPPORT_PREFERENCE"
        android:title="Location Support"
        android:summaryOn="Uncheck this if you don't want to receive push notifications for alerts in your area (2km radius)."
        android:summaryOff="Filters locations that are more than 15 km away. Uncheck this if you want to see all alerts."
        android:defaultValue="true" />