how to use getSharedPreferences in android

First get the instance of SharedPreferences using SharedPreferences userDetails = context.getSharedPreferences(“userdetails”, MODE_PRIVATE); Now to save the values in the SharedPreferences Editor edit = userDetails.edit(); edit.putString(“username”, username.getText().toString().trim()); edit.putString(“password”, password.getText().toString().trim()); edit.apply(); Above lines will write username and password to preference Now to to retrieve saved values from preference, you can follow below lines of code String userName … Read more

How to get all keys of SharedPreferences programmatically in Android?

SharedPreferences has the method getAll() that returns a Map<String, ?> . From the Map you can retrieve easily the keys with keySet() and the key/value mappings with entrySet(): Map<String, ?> allEntries = prefA.getAll(); for (Map.Entry<String, ?> entry : allEntries.entrySet()) { Log.d(“map values”, entry.getKey() + “: ” + entry.getValue().toString()); }

Is it possible to add an array or object to SharedPreferences on Android

Regardless of the API level, Check String arrays and Object arrays in SharedPreferences SAVE ARRAY public boolean saveArray(String[] array, String arrayName, Context mContext) { SharedPreferences prefs = mContext.getSharedPreferences(“preferencename”, 0); SharedPreferences.Editor editor = prefs.edit(); editor.putInt(arrayName +”_size”, array.length); for(int i=0;i<array.length;i++) editor.putString(arrayName + “_” + i, array[i]); return editor.commit(); } LOAD ARRAY public String[] loadArray(String arrayName, Context mContext) … Read more

How to listen for preference changes within a PreferenceFragment?

I believe you just need to register/unregister the Listener in your PreferenceFragment and it will work. @Override public void onResume() { super.onResume(); getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); } @Override public void onPause() { getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this); super.onPause(); } Depending on what you want to do you may not need to use a listener. Changes to the preferences are committed to SharedPreferences … Read more

Android preferences onclick event

You need to set android:key for the item: <Preference android:key=”myKey” android:title=”About” /> Then you can do the following in your code: Preference myPref = (Preference) findPreference(“myKey”); myPref.setOnPreferenceClickListener(new OnPreferenceClickListener() { public boolean onPreferenceClick(Preference preference) { // open browser or intent here return true; } });

how to remove shared preference while application uninstall in android

The problem is not with preferences. It’s drastically the backup manager! .. since android-23 by default backup as a task stores app’s data including preferences to cloud. Later when you uninstall then install newer version you are probably going to use restored preferences. To avoid that, just add this to your manifest (or at least … Read more

How to implement a confirmation (yes/no) DialogPreference?

That is a simple alert dialog, Federico gave you a site where you can look things up. Here is a short example of how an alert dialog can be built. new AlertDialog.Builder(this) .setTitle(“Title”) .setMessage(“Do you really want to whatever?”) .setIcon(android.R.drawable.ic_dialog_alert) .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Toast.makeText(MainActivity.this, “Yaay”, Toast.LENGTH_SHORT).show(); }}) … Read more

How to add a button to a PreferenceScreen?

For the xml: <Preference android:title=”Acts like a button” android:key=”@string/myCoolButton” android:summary=”This is a cool button” /> Then for the java in your onCreate() Preference button = findPreference(getString(R.string.myCoolButton)); button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { //code for what you want it to do return true; } }); This will appear like a normal Preference, with … Read more

Difference between getDefaultSharedPreferences and getSharedPreferences

getDefaultSharedPreferences will use a default name like “com.example.something_preferences”, but getSharedPreferences will require a name. getDefaultSharedPreferences in fact uses Context.getSharedPreferences (below is directly from the Android source): public static SharedPreferences getDefaultSharedPreferences(Context context) { return context.getSharedPreferences(getDefaultSharedPreferencesName(context), getDefaultSharedPreferencesMode()); } private static String getDefaultSharedPreferencesName(Context context) { return context.getPackageName() + “_preferences”; } private static int getDefaultSharedPreferencesMode() { return Context.MODE_PRIVATE; }

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)