1

I have ListPreference which I want to use it for enable/disable multiple preferences which are all under PreferencesScreen/PreferencesCategory (in same activity), so how to do that in one shot?

is there an easy way to disable or enable a grouped preferences depending on ListPreference value in another PreferenceScreen or PreferenceCategory ?

description to generate similar issue:

I am using eclipse, however, similar steps are existed in android studio.

  1. create new project, click new -> android application project -> write any name for this test project and press next(s) then finish.
  2. go to menu: file -> new -> other click on Android object then choose settings then click on next(s) till finish as show bellow.

enter image description here

enter image description here

enter image description here

after finish click, if any errors appears recompile the project from menu: project -> clean .

now the created project have settings and we can call it by add two lines in MainActivity.java in onOptionsItemSelected method to be like this:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            Intent i = new Intent (this, SettingsActivity.class);
            startActivity(i);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

run the app and click on settings in action bar to open settings activity

enter image description here

finally the settings will appears, and contains 3 preferences screens as following:

enter image description here

look at "add friends to messages" it is in pref_general, what I want is when user change it's value to "never" the all preferences under pref_data_sync should be disabled, while if the new value is "always" then all preferences in pref_notification should disabled otherwise enable all preferences in both screen preferences.

Eng. Samer T
  • 6,465
  • 6
  • 36
  • 43

1 Answers1

1

1.) You implement a OnSharedPreferenceChangeListener in your SettingsActivity.java

public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener
{

       //override this method
       public void onSharedPreferenceChanged (SharedPreferences  sharedPreferences, String key)
       {

       }    
}

2.) Now check in this method for the value of "add friends" preference.

public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener
{

       //override this method
       public void onSharedPreferenceChanged (SharedPreferences  sharedPreferences, String key)
       {

       String add_friends = PreferenceManager.getDefaultSharedPreferences(context).getString(
                            "add_friends", null);
       if(add_friends.equalsIgnoreCase("never")
       {
           // Get the screen you want to remove
           PreferenceScreen preferenceScreen = (PreferenceScreen) findPreference("pref_screen");


           //remove the other screens.  
           PreferenceGroup preferenceScreenParent = getParent(preferenceScreen);
           preferenceScreenParent.removePreference(preferenceScreen);
       }

       } 

}
cafebabe1991
  • 4,928
  • 2
  • 34
  • 42
  • thanks for reply, removePreference will remove preferences, what I am looking for is something like dependencies related to ListPreferene, which makes the preferences under another preferenceScreen in gray font and user can see it however can't change its values till listPref has another value. another point is: if you try the above steps, you will have SettingsActivity with interface object sBindPreferenceSummaryToValueListener this is an instance of OnPreferenceChangeListener, we can use it instead of implementing OnSharedPreferenceChangeListener to class, so do you have new ideas? – Eng. Samer T Mar 15 '15 at 12:00
  • So you want to disable it instead of removing it ? – cafebabe1991 Mar 15 '15 at 13:13