onPreferenceChange method isn't called when Listpreferences changes - android

I have a Preferences Activity in my code.
I want to show a toast when ever user change radio button in a ListPreferences.
I used this code but it does not work. :(
public class PrefsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
public void onSharedPreferenceChanged(SharedPreferences arg0, String key) {
ListPreference listPreference = (ListPreference) findPreference(key);
listPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// TODO Auto-generated method stub
Toast.makeText(PrefsActivity.this, "second", Toast.LENGTH_LONG).show();
return false;
}
});
}
}
what is my problem?

Change your onCreate code to:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);
}
You are not adding the registerOnSharedPreferenceChangeListener to PrefsActivity Activity.

Related

onsharedpreferencechangelistener not work in android app

I wrote this code that works with sharedPreference ,
in sosbutton I changed sharedPreference ,I want when sharedPreference changed ,
onsharedpreferencechangelistener fire .
I wrote this code but onsharedpreferencechangelistener didn't fire.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SharedPreferences prefs = getSharedPreferences("demopref", Context.MODE_WORLD_READABLE);
final Button sosBtn=(Button) findViewById(R.id.button);
sosBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String data = prefs.getString("demostring", "No Value");
SharedPreferences.Editor editor = prefs.edit();
editor.putString("demostring","HI...");
editor.commit();
////////////////////////////////
}
});
SharedPreferences.OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
Toast.makeText(getApplicationContext(),"changed : " + key,Toast.LENGTH_LONG).show();
}
};
prefs.registerOnSharedPreferenceChangeListener(listener);
}
}
please help me :(
thanks in advance
I solved my problem:
public class MainActivity extends Activity {
SharedPreferences pref;
SharedPreferences.OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
#Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if(key.equals("demopref"))
Toast.makeText(getApplicationContext(),"Hello2---"+key,Toast.LENGTH_LONG).show();
}
};
#SuppressWarnings("deprecation")
int i=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
////////////////////////////////////////
final Button sosBtn = (Button) findViewById(R.id.button);
sosBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences.Editor editor = pref.edit();
editor.putString("demopref","HI"+String.valueOf(i));
i++;
editor.commit();
/////////////////////////////////
}
});
}
#Override
protected void onResume() {
pref.registerOnSharedPreferenceChangeListener(listener);
super.onResume();
}
#Override
protected void onPause() {
pref.unregisterOnSharedPreferenceChangeListener(listener);
super.onPause();
}
}
MODE_WORLD_READABLE is deprecated, try MODE_PRIVATE
Try this may be help you...
your not given reference to your
SharedPreferences.OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
here is my code
public class MainActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SharedPreferences prefs = getSharedPreferences("demopref", Context.MODE_PRIVATE);
final Button sosBtn=(Button) findViewById(R.id.button);
sosBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String data = prefs.getString("demostring", "No Value");
SharedPreferences.Editor editor = prefs.edit();
editor.putString("demostring","HI...");
editor.commit();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(listener);
////////////////////////////////
}
});
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
Toast.makeText(getApplicationContext(),"changed : " + key,Toast.LENGTH_LONG).show();
}
};
prefs.registerOnSharedPreferenceChangeListener(listener);
}
}

How to get value from PreferenceFragment to another Activity

I need to get value from PreferenceFragment to SplashActivity. How can i do it?
public class SettingsFragment extends PreferenceFragment {
public String cityToLoad;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
ListPreference city = (ListPreference) findPreference("city");
city.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
cityToLoad = newValue.toString();
return true;
}
});
}}
my preferences.xml contains only ListPreference.
You don't have onchange event in list preference. For More http://developer.android.com/reference/android/preference/PreferenceFragment.html
And if you are just using preference tag then you can try below code.
city.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference arg0) {
Intent newScreen = new Intent(getActivity(), SecondActivity.class);
newScreen.putExtra("Data", data);
startActivity(newScreen);
return true;
}
});

Changing CheckBoxPreference inside preference activity

I need to change Check Box Preference inside the Preference activity, but it not seems to work:
public class SettingsActivity extends PreferenceActivity {
static CheckBoxPreference autoP;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public static class MyPreferenceFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener{
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
autoP = (CheckBoxPreference)findPreference("autoP");
autoP.setOnPreferenceChangeListener(autoP_listener);
OnPreferenceChangeListener autoP_listener = new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference,Object newValue) {
autoP.setDefaultValue((String) newValue);
if (newValue.toString().equals("true"))
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
else
getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
return false;
}
};
}
}
This should work:
final CheckBoxPreference autoP = (CheckBoxPreference) getPreferenceManager().findPreference("autoP");
autoP.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
if ((Boolean)newValue == true) {
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}else {
getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
return true;
}
});
try this:
public class SettingsActivity extends PreferenceActivity {
static CheckBoxPreference autoP;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public static class MyPreferenceFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
autoP = (CheckBoxPreference)findPreference("autoP");
autoP.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference,Object newValue) {
updateOnPreferenceChange();
}
});
}
private void updateOnPreferenceChange() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
String pref_name = "should_be_pref_name_in_xml_file";
boolean checked = sharedPreferences.getBoolean(pref_name, defaultValue);
if (checked) {
// todo
} else {
// todo
}
}
}
}
Try this
public class SettingsActivity extends PreferenceFragment {
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
#Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
Preference preference) {
String key = preference.getKey();
if (key.equals("key")) {
if (((CheckBoxPreference) preference).isChecked()){
// do things if the checkbox is checked
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
} else {
// do things if it is unchecked
getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
return true;
}
}

onPreferenceClick and OnPreferenceClickListener

I'm attempting to evaluate my preferences in my java code in order to enable/disable other options it they chose not to do other options... So far i'm trying to only implement the OnPreferenceClickListener however i never see the toast from the changes. What am i doing wrong? There seem to be alot of other questions like this but i cannot see my error in reference to them.
public class UserSettingActivity extends PreferenceActivity implements OnPreferenceClickListener{
SharedPreferences mPreferences;
Boolean frequency;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
#Override
public boolean onPreferenceClick(Preference preference) {
mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
frequency = mPreferences.getBoolean("frequency", true);
Context context = getApplicationContext();
Toast.makeText(context, "Hello toast 0!", Toast.LENGTH_LONG).show();
if (!frequency) {
Context context2 = getApplicationContext();
Toast.makeText(context2, "Hello toast 1!", Toast.LENGTH_LONG).show();
} else if (preference.getKey().equals("schedulestop")) {
} else if (preference.getKey().equals("priority")) {
} else {
Context context3 = getApplicationContext();
Toast.makeText(context3, "Hello toast 0!", Toast.LENGTH_LONG).show();
}
return false;
}
}
You have to register for PreferenceClickListener each individual preference
somePreference.setOnPreferenceClickListener(this);
or you can use getSharedPreferences().registerOnSharedPreferenceChangeListener for all preferences.
public class UserSettingActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener{
SharedPreferences mPreferences;
Boolean frequency;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
#SuppressWarnings("deprecation")
#Override
protected void onPause()
{
super.onPause();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
#SuppressWarnings("deprecation")
#Override
protected void onResume()
{
super.onResume();
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
#SuppressWarnings("deprecation")
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals("schedulestop")) {
// do something
}
else if (key.equals(......
}
}
first implement "OnSharedPreferenceChangeListener"
PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener
Then
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
settings.registerOnSharedPreferenceChangeListener(this);
}
THEN
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
switch (key) {
case "pref_key_auto_delete":
boolean notificationStatus = SP.getBoolean(key, false);
Log.d("stat", String.valueOf(notificationStatus));
break;
case "pref_key_notification_list":
String downloadType = SP.getString(key, "n/a");
Log.d("stat", String.valueOf(downloadType));
break;
}

Edit text preference

I have made edit preference but I am not getting onchangelistener when clicking on ok button as I use the ondismiss listener but when comment that code I get onchange listener but after uncommenting it I am not getting onchangelistener.
public class Preferences extends PreferenceActivity{
SharedPreferences sharedPreferences=null;
String text=null;
String negativeBtnTxt=null;
private EditTextPreference editTextPreference=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference_layout);
sharedPreferences=PreferenceManager.getDefaultSharedPreferences(getBaseContext());
Preference preference=(Preference)findPreference("customPref");
editTextPreference=(EditTextPreference)findPreference("editTextPref");
editTextPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
Log.v("in Preferences","edit text pref onclick");
dialogActivated();
return false;
}
});
editTextPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Log.v("in Preferences","edit text pref changed");
return false;
}
});
Log.v("edit text dialog=","---"+editTextPreference.getDialog());
Log.v("ET value is=",""+sharedPreferences.getString("editTextPref", "Hello!!!"));
preference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
Toast.makeText(getApplicationContext(), "Clicked on preference", 3).show();
Log.v("in Preferences","");
return false;
}
});
}
private void dialogActivated()
{
if(editTextPreference.getDialog()!=null)
{
editTextPreference.getDialog().setCancelable(true);
editTextPreference.getDialog().setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
Log.v("in Preferences","back key cancel");
}
});
editTextPreference.getDialog().setOnDismissListener(new OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
Log.v("in Preferences","dismis called");
editTextPreference.getNegativeButtonText();
}
});
}
}
}

Categories

Resources