0

I am beyond frustrated in why my preferences are not doing anything. Currently I have it set up in an OnSharedPreferenceChangeListener:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new SettingsFragment())
            .commit();

    PreferenceManager.setDefaultValues(this, R.xml.preferences_new, false);

    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);

}
    public void onSharedPreferenceChanged(
            SharedPreferences sharedPreferences, String key) {
        sharedPreferences.registerOnSharedPreferenceChangeListener(this);
        // TODO Auto-generated method stub
        if (key.equals("password")) {
            LayoutInflater inflater = LayoutInflater.from(this);
            final View text = inflater.inflate(R.layout.changepassword, null);
            final EditText currentPassword = (EditText)text.findViewById(R.id.currentPassword);
            final EditText newPassword = (EditText)text.findViewById(R.id.newPassword);

            final AlertDialog.Builder alert = new AlertDialog.Builder(this);
            alert.setTitle("Change Your Password");
            alert.setView(text);
            alert.setPositiveButton("Change", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                        String stringData = currentPassword.getText().toString().trim();
                        String stringNew = newPassword.getText().toString().trim();
                        dataReturned = myFolder.getString("passwordKey", "");
                        if(dataReturned.equals(stringData)) {                       
                            String newData = newPassword.getText().toString().trim();
                            SharedPreferences.Editor editor = myFolder.edit();
                            editor.putString("passwordKey", newData);
                            editor.commit();
                            dataReturned  = myFolder.getString("passwordKey", "couldn't load data");
                            Toast.makeText(getApplicationContext(), "Password changed", Toast.LENGTH_SHORT).show();
                            currentPassword.setText("");
                            newPassword.setText("");
                        }

                        else {
                            Toast.makeText(getApplicationContext(), "Incorrect Password", Toast.LENGTH_LONG).show();
                            currentPassword.setText("");
                            newPassword.setText("");
                        }
                    }
                });
            alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    dialog.cancel();
                }
                });
            alert.show();
            ;
        }
        if (key.equals("notification")) {
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.device_access_secure)
            .setOngoing(true)
            .setContentTitle("Obstruct")
            .setContentText("Start Stealth Mode");

            Intent resultIntent = new Intent(this, MainActivity.class);

            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            stackBuilder.addNextIntent(resultIntent);

            PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
            mBuilder.setContentIntent(resultPendingIntent);

            NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.notify(0, mBuilder.build());
        }
        }

}   

Here's my PreferenceFragment:

public class SettingsFragment extends PreferenceFragment {

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

    addPreferencesFromResource(R.xml.preferences_new);
}


}

Here's my preference xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="preferenceScreen" >
<PreferenceCategory android:title="@string/pref_password">
    <Preference 
        android:title="@string/pref_password" 
        android:summary="@string/pref_change_password" 
        android:key="password"
        android:enabled="true"/>

</PreferenceCategory>
<PreferenceCategory 
    android:title="@string/pref_notif">
    <CheckBoxPreference 
        android:title="@string/pref_enable_disable" 
        android:summary="@string/pref_enable_check" 
        android:key="notification" 
        android:defaultValue="true"
        android:enabled="true"/>
</PreferenceCategory>

Every time I click on one of my preferences it doesn't do anything. Am I missing something or is there an easier way?

user2759839
  • 305
  • 1
  • 3
  • 11
  • it doesnt look like your implementing it correctly, i never tried it but check this out http://stackoverflow.com/questions/2542938/sharedpreferences-onsharedpreferencechangelistener-not-being-called-consistently – JRowan Nov 18 '13 at 23:04
  • You need to register your preferences with preference change listener: http://developer.android.com/reference/android/preference/Preference.html#setOnPreferenceChangeListener(android.preference.Preference.OnPreferenceChangeListener) – ozbek Nov 18 '13 at 23:43
  • Tried both. The preferences still do nothing. – user2759839 Nov 19 '13 at 21:37

1 Answers1

0

It seems you are missing preference_headers.xml file. The preference header xml file should then point to preference fragment in the android:fragment parameter - the class that extends PreferenceFragment in your application:

<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">

<header android:fragment="com.example.myClassThatExtendsPreferenceFragment"
            android:title="Preferences"
            android:summary="Application Preferences">
        <extra android:name="resource" android:value="preferences" />
</header>

</preference-headers>

If you forget to add android:fragment parameter, your application won't react to on click event, but won't throw any error either.

Then you should implement xml that should be named according to extra attribute within the header tag - that is preferences.xml (according to android:value parameter within extra tag), e.g.:

<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Simple Preferences">
    <CheckBoxPreference
        android:key="checkbox"
        android:title="Checkbox Preference"
        android:summary="Sample checkbox preference"
    />
</PreferenceCategory>
</PreferenceScreen>

This holds your preferences. Then, you need to implement class that extends PreferenceActivity - that is class that allows you to edit Preferences. This class should implement/override at minimum onBuildHeaders method, which can look like this:

public class EditPreferences extends PreferenceActivity {
@Override
public void onBuildHeaders(List<Header> target) {
    loadHeadersFromResource(R.xml.preference_headers, target);
}

It simply points to the xml file that defines preference headers.

Finally, when referring to the preferences in your class that extends PreferenceFragment, you could use this syntax:

int res=getActivity().getResources().getIdentifier(getArguments().getString("resource"), "xml",getActivity().getPackageName());
    addPreferencesFromResource(res);

where you simply get the content of your preferences file, denoted by the android:name tag under your preference header.

I hope that helps.

Tomas Antos
  • 284
  • 2
  • 6