2

I've set up a Preference Screen as a settings option in my App, but I am totally confused about how to make the changes done when user selects or changes an Option.

Here's the xml code of my PreferenceScreen:

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

    <PreferenceCategory android:title="Notifications">
        <RingtonePreference
            android:summary="Choose a Ringtone"
            android:title="Notification Ringtone"
            android:key="ringtonePref" />
        <ListPreference
            android:title="Notification Timer"
            android:summary="Select when to Notify"
            android:dialogTitle="Show Notification after every:"
            android:positiveButtonText="OK"
            android:negativeButtonText="Cancel"
            android:entries="@array/entries"
            android:entryValues="@array/entries"
            android:key="listPrefs" />
        <ListPreference
            android:title="LED Color"
            android:summary="Choose LED Color"
            android:positiveButtonText="OK"
            android:negativeButtonText="Cancel"
            android:entries="@array/colors"
            android:entryValues="@array/colors"
            android:key="listPrefs2" />
        <CheckBoxPreference
            android:title="LED"
            android:defaultValue="true"
            android:summary="Flash LED or Not"
            android:key="checkBoxPrefs2"/>
        <CheckBoxPreference
            android:title="Vibrate"
            android:defaultValue="true"
            android:summary="Vibrate on receiving Notification"
            android:key="checkBoxPrefs3" />
    </PreferenceCategory>

    <PreferenceCategory android:title="Invite">
        <Preference android:summary="Send invitation to more peoples so that\nthey can also Learn more and more Duas."/>
    </PreferenceCategory>

    <PreferenceCategory android:title="Version">
        <Preference
            android:key="versionPrefs"
            android:summary="Version 1.0\nThank you for Downloading the App." />
    </PreferenceCategory>

</PreferenceScreen>

and here is my Settings.class:

public class Settings extends PreferenceActivity implements OnPreferenceChangeListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);

        //setting the properties of Action Bar
        ActionBar actionBar = getActionBar();
        actionBar.setTitle(Html.fromHtml("<font color='#FFFFFF'><strong>Settings</strong></font>"));
        actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#33B5E5")));
        actionBar.setDisplayHomeAsUpEnabled(true);

        PreferenceManager.setDefaultValues(this, R.xml.settings, true);
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        // TODO Auto-generated method stub
        return false;
    }
}

I don't know what code should I add to onPreferenceChange to make my Preferences actually do the change.

Please tell me.

Any help would be highly Appreciated.

Thanks for your precious time.

Hammad Nasir
  • 153
  • 1
  • 10
  • http://developer.android.com/reference/android/preference/package-summary.html – Shailendra Madda Jul 26 '14 at 13:27
  • @Hammad Nasir If u want to save ur settings use SharedPreferences Please check the following link http://developer.android.com/reference/android/content/SharedPreferences.html – Akshay Mukadam Jul 26 '14 at 13:33
  • @shylendra I want something like suppose there is an option given of Vibration, if user checks it than vibration will happen and if user will uncheck it the vibration will not happen. Please help me with this. – Hammad Nasir Jul 26 '14 at 14:27

3 Answers3

2

For vibration

CheckBoxPreference checkBoxPrefs3 = (CheckBoxPreference) findPreference("checkBoxPrefs3");
    checkBoxPrefs3.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object o) {
            if ((Boolean) o) {
                checkBoxPrefs3.setChecked(true); 
                // vibration for 800 milliseconds
                ((Vibrator)getSystemService(VIBRATOR_SERVICE)).vibrate(800);
                // TODO vibrate
            } else {
                checkBoxPrefs3.setChecked(false);
            }
            return false;
        }
    });

Required permission to add inside the AndroidManifest.xml file:

<uses-permission android:name="android.permission.VIBRATE" />

How to Apply For your App?

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean vibrate = sharedPrefs.getBoolean("checkBoxPrefs3", true);

Notification notification = new Notification(icon,
                    "message", when);
            notification.defaults |= Notification.DEFAULT_SOUND;
            if (vibrate) {
                   notification.defaults |= Notification.DEFAULT_VIBRATE;
            }

            notification.setLatestEventInfo(mContext,
                    "ticker",
                    "message", contentIntent);
            notification.flags = Notification.FLAG_AUTO_CANCEL;
            notificationManager.notify(count, notification);
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
  • You did'nt get me bro, I want to set this as a _setting of my App that when the user **checks** the vibration checkbox the device will vibrate when notification will be received and if user **unchecks** it the device will not vibrate when Notification is received._ Please **help** with this too. – Hammad Nasir Jul 28 '14 at 06:39
  • **Thanks for this answer** bro, it **Worked!** Now please tell me about _ListPreferences_ also. I have posted in my question, there I have given an option that after how much hours the users would like to get Informed. for ex - after every 2 hours or 4 hours and so on. _So for ex- if a user selects 2 hours **how should the App be able to notify user after every 2 hours? Thanks again. Please HELP with this also**._ – Hammad Nasir Jul 28 '14 at 10:10
  • set alarm in preference change – Biraj Zalavadia Jul 28 '14 at 10:15
  • But bro Alarm would be set for only a particular interval and I want it to set for 5 different time intervals as user's choice. How to? – Hammad Nasir Jul 28 '14 at 10:19
  • you need to set five alarm for with seleceted time interval – Biraj Zalavadia Jul 28 '14 at 10:28
  • OK, and how to give references like if user selects 2 hours then alarm will fire after every 2 hours or if user selects 8 hours then alarm will fire after every 8 hours. – Hammad Nasir Jul 28 '14 at 10:29
  • use serRepeat() method to play alarm repeatedly – Biraj Zalavadia Jul 28 '14 at 10:31
  • You didn't got it, this I know how to repeat Alarms but I am asking that how to set it accordingly for ex-if user selects to notify him/her after every 8 hours, so how would App fire Alarm after every 8 hours and then if same user changes it to 10 hours than how would the Alarm be fired after every 10 hours. This is what I am asking. Please tell me how to do this? – Hammad Nasir Jul 28 '14 at 10:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/58152/discussion-between-biraj-zalavadia-and-hammad-nasir). – Biraj Zalavadia Jul 28 '14 at 10:42
1

On your PreferenceActivity

  • Initialize your CheckBoxPreference
  • Add OnPreferenceChangeListener
  • Do whatever you want on change..

Here's an example:

    CheckBoxPreference checkBoxPrefs3 = (CheckBoxPreference) findPreference("checkBoxPrefs3");
    checkBoxPrefs3.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object o) {
            if ((Boolean) o) {
                checkBoxPrefs3.setChecked(true); 
                // TODO vibrate
            } else {
                checkBoxPrefs3.setChecked(false);
            }
            return false;
        }
    });
mroczis
  • 1,879
  • 2
  • 16
  • 18
  • Thanks for the answer, but please tell more briefly. What to do here: `// TODO vibrate` . _What code should be written here in order to vibrate when **checked** and don't vibrate when **unchecked**._ – Hammad Nasir Jul 26 '14 at 23:56
  • Please help with this. – Hammad Nasir Jul 27 '14 at 08:45
  • You did'nt get me bro, I want to set this as a setting of my App that when the user checks the vibration checkbox the device will vibrate when notification will be received and if user unchecks it the device will not vibrate when Notification is received. Please help with this too. – Hammad Nasir Jul 28 '14 at 07:17
0

Android saves it automatically to your SharedPreferences. You can always get the desired value by calling:

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getContext());
boolean led = sharedPrefs.getBoolean("checkBoxPrefs2", true);

Where checkBoxPrefs2 is key defined in your layout and true is default value.

mroczis
  • 1,879
  • 2
  • 16
  • 18
  • Thanks for this answer. But I want something like suppose there is an option given of Vibration, _if user checks it than vibration will happen and if user will uncheck it the vibration will not happen_. Please **help** me with this. – Hammad Nasir Jul 26 '14 at 14:26