I'm struggling to figure out how to show the preference in the preference activity. I'm simply trying to show the high score and i keep getting the same 345 value show up. I have confirmed in debugging that the new high score is saved . Here is all my code.
preferences.xml
<Preference
android:title="High Score"
android:selectable="false"
android:defaultValue="0"
android:key="#string/pref_highest_score"/>
HighScoreActivity.java
public class HighScoreActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new HighScoreFragment()).commit();
}
public static class HighScoreFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
#Override
public void onResume(){
super.onResume();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
}
}
}
strings.xml
<!-- High Score settings -->
<string name="pref_highest_score">345</string>
<string name="title_activity_high_score">High Score</string>
MainActivity.java
preferences = PreferenceManager.getDefaultSharedPreferences(context);
Assets.highScore = preferences.getInt(context.getString(R.string.pref_highest_score), 1);
editor = preferences.edit();
editor.putInt(context.getString(R.string.pref_highest_score), currentPoints );
editor.apply();
It's because your preference screen does not actually gets the value of pref_highest_score. You should find the preference and call Preference.setSummary(). Like:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
// Gets the preference defined in XML
Preference p = findPreference("#string/pref_highest_score");
// Sets the summary of the preference to the high score
p.setSummary(
String.valueOf(preferences.getInt(context.getString(R.string.pref_highest_score), 1))
);
}
where preferences is PreferenceManager.getDefaultSharedPreferences(context).
And in your XML, specify an id of the preference:
<Preference
android:key="#string/pref_highest_score"
android:title="High Score"
android:selectable="false"
android:defaultValue="0"/>
Related
I am a beginner in android and I have a confusion regarding Shared Preferences implementation. My goal is to use a string where a user defines some text which will be used in MainFragment. Given the fact that user may change that string when application is running I need a listener as well. So according to one book so far I have a SettingsActivity and a SettingsFragment.
SettingsActivity so far:
public class SettingsActivity extends AppCompatActivity {
private SharedPreferences prefs;
private String stringIWantToSave;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
getFragmentManager().beginTransaction().
replace(android.R.id.content, new SettingsFragment(), "settings_fragment").commit();
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
}
#Override
protected void onResume() {
super.onResume();
stringIWantToSave = prefs.getString("stringIWantToSave", "myString");
}
}
SettingsFragment so far:
public class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
private SharedPreferences sharedPreferences;
private String stringIWantToSave;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
}
#Override
public void onPause() {
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
super.onPause();
}
#Override
public void onResume() {
super.onResume();
sharedPreferences.getString("stringIWantToSave", "myString");
sharedPreferences.unregisterOnSharedPreferenceChangeListener(this);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
}
}
My questions are:
• In which method and how should I save the changed value from the user?
• How can I implement the listener so that it will inform the MainFragment that the string has changed?
1. To achieve the SettingsActivity with EditTextPreference, first you have to create a PreferenceScreen that contains EditTextPreference.
preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference
android:key="pref_key_name"
android:title="Name"
android:summary="Enter your name here!"
android:dialogTitle="Enter name:">
</EditTextPreference>
</PreferenceScreen>
2. Create a Fragment extending PreferenceFragment. Do preference initialization and add OnSharedPreferenceChangeListener to update the UI when user input their name.
SettingsFragment.java:
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
public class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
// Preference Keys
public static final String KEY_PREF_NAME = "pref_key_name";
// Shared preference
SharedPreferences mSharedPreferences;
// Name preference
EditTextPreference mPreferenceName;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
// Shared preference
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
// Name preference
mPreferenceName = (EditTextPreference) getPreferenceScreen().findPreference(KEY_PREF_NAME);
// Initialize
initPreferences();
}
public void initPreferences()
{
// Name
String oldName = mSharedPreferences.getString(KEY_PREF_NAME, "Enter your name here!");
// Update view
mPreferenceName.setSummary(oldName);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPref, String key) {
if(key.equals(KEY_PREF_NAME))
{
// Name
String currentName = sharedPref.getString(key, "DEFAULT_VALUE");
// Update view
mPreferenceName.setSummary(currentName);
}
}
#Override
public void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
}
3. Finally, Create SettingsActivity and show SettingsFragment on its FrameLayout.
SettingsActivity.java:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
public class SettingsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
// Settings Fragment
SettingsFragment settingsFragment = new SettingsFragment();
getFragmentManager().beginTransaction().replace(R.id.content, settingsFragment).commit();
}
#Override
protected void onResume() {
super.onResume();
}
}
HOW TO USE:
To use updated name from preference, get the name value from preference inside your activity or fragments onResume() method:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onResume() {
super.onResume();
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
// Name
String name = sharedPreferences.getString(SettingsFragment.KEY_PREF_NAME, "DEFAULT_VALUE");
// Do something with name
................
........................
}
}
OUTPUT:
Hope this will help~
i have setup an onpreference click listener for a preference but it fails to fire even though the preference is found by the find preference command.
Activity
public class PreferenceActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Display the fragment as the main content
setContentView(R.layout.activity_preference);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.replace(R.id.settings, new SettingsFragment())
.commit();
}
}
public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
Context context;
private final String DOB = "date";
private SharedPreferences prefs;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
context = getActivity().getApplicationContext();
Preference dob = findPreference(DOB);
Log.i("test", dob.getKey() + "");
dob.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
Log.i("test", "2");
return false;
}
});
}
#Override
public void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
#Override
public void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
}
}
The preference xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="My Account">
<EditTextPreference
android:key="profile_name"
android:summary="Taylor Swift"
android:title="Username" />
<Preference
android:key="date"
android:summary="N.A."
android:title="#string/date" />
</PreferenceCategory>
</PreferenceScreen>
The log output for "test" correctly displays the key of the preference but the log within the onclick never fires.
I've created simple project with your code and everything seems to be working fine.
Log.i("test", "2");
is being called when I click "Date" field.
I am a newbie at android development and I am trying to implement a Settings Activity so that it can take user preferences. This is my Settings Activity which already includes code to handle Edit Preference changes.The module is implemented for Android 3.0 and above hence Settings Fragment is used.
public class SettingsActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
}
public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.pref_general);
}
#Override
public void onResume(){
super.onResume();
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onPause(){
super.onPause();
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,String key){
if(key.equals("location")){
Preference loc_preference=findPreference("location");
loc_preference.setSummary(sharedPreferences.getString(key,""));
}
}
}
}
This is the pref_gen.xml file
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:key="#string/pref_loc_key"
android:dialogMessage="#string/location_dialog_message"
android:dialogTitle="#string/location_dialog"
android:title="#string/pref_loc_label"
android:defaultValue="#string/pref_default_loc"
android:selectAllOnFocus="true"
android:singleLine="true" />
<ListPreference
android:key="#string/pref_unit_key"
android:dialogTitle="#string/unit_dialog_message"
android:title="#string/pref_unit_label"
android:defaultValue="#string/pref_default_unit"
android:entries="#array/pref_unit_options"
android:entryValues="#array/pref_unit_values" />
This is the arrays.xml file
<string-array name="pref_unit_options">
<item>Metric</item>
<item>Imperial</item>
</string-array>
<string-array name="pref_unit_values">
<item>1</item>
<item>0</item>
</string-array>
Can anyone help me to add the List Preference module in the Settings Fragment method along with a listener? I have gone through other posts but somehow I cant modify it accordingly.
You can use getPreference to reference ListPreference in java and then register listener on it.
ListPreference unit_preference = mContext.getPreference(<Key>);
unit_preference .setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
public boolean onPreferenceChanged(Preference preference, Object newValue) {
//Do something with newValue here
}
});
Note: mContext is a class variable of type Context. You must set it before calling beginTransaction:
mContext = this;
I have prepared a simple test case at GitHub for my question and have a feeling, that only few lines of code are missing to make it working.
In an Android app I have 1 activity (MainActivity.java) and 2 fragments (MainFragment.java and PrefFragment.java) - and when I change values in the preferences fragment, they are not saved.
Also the default values are not loaded and the summaries not updated:
Here is my PrefFragment where I register a shared prefs listener - in order to update the summaries - but for some reason it is never called:
public class PrefFragment extends PreferenceFragment
implements OnSharedPreferenceChangeListener {
public static final String BOOL_1 = "bool_1";
public static final String STR_1 = "str_1";
public static final String STR_2 = "str_2";
private CheckBoxPreference mBool1;
private EditTextPreference mStr1;
private EditTextPreference mStr2;
private SharedPreferences mPrefs;
private Editor mEditor;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(false);
addPreferencesFromResource(R.xml.preferences);
mBool1 = (CheckBoxPreference) findPreference(BOOL_1);
mStr1 = (EditTextPreference) findPreference(STR_1);
mStr2 = (EditTextPreference) findPreference(STR_2);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mPrefs = PreferenceManager.getDefaultSharedPreferences(activity);
mEditor = mPrefs.edit();
mPrefs.registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onDetach() {
super.onDetach();
mPrefs.unregisterOnSharedPreferenceChangeListener(this);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences prefs,
String key) {
Log.d("onSharedPreferenceChanged", key); // XXX is never called
if (BOOL_1.equals(key)) {
boolean bool1 = prefs.getBoolean(key, false);
mBool1.setSummary(bool1 ? "Enabled" : "Disabled");
} else if (STR_1.equals(key)) {
String str1 = prefs.getString(key, "");
mStr1.setSummary(str1);
} else if (STR_2.equals(key)) {
String str2 = prefs.getString(key, "");
mStr2.setSummary(str2);
}
}
}
And here is preference.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:title="Category 1"
android:key="category_1">
<CheckBoxPreference
android:key="bool_1"
android:title="Boolean 1"
android:summary="XXX how to set this? XXX"
android:defaultValue="false" />
<EditTextPreference
android:dependency="bool_1"
android:key="str_1"
android:title="String 1"
android:summary="XXX how to set this? XXX"
android:defaultValue="change me" />
<EditTextPreference
android:dependency="bool_1"
android:key="str_2"
android:title="String 2"
android:summary="XXX how to set this? XXX"
android:defaultValue="change me too" />
</PreferenceCategory>
</PreferenceScreen>
So my question is how to handle (load, save) values in PreferenceFragment? Do I really have to extend EditTextPreference and implement onDialogClosed method in my custom class? This seems like an overkill approach to me.
Ok, the following seems to work in PrefFragment.java:
#Override
public void onResume() {
super.onResume();
SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
// CHANGE 1: load saved values to set the summaries
onSharedPreferenceChanged(prefs, BOOL_1);
onSharedPreferenceChanged(prefs, STR_1);
onSharedPreferenceChanged(prefs, STR_2);
// CHANGE 2: register shared prefs listener in onResume
prefs.registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onPause() {
super.onPause();
SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
prefs.unregisterOnSharedPreferenceChangeListener(this);
}
How would I add a listener so that when a preference is changed (i.e. CheckBoxPreference) some
method is executed (i.e. Toast)
Preferences.java
public class Preferences extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
Main.java
[...]
SharedPreferences preferences;
preferences = PreferenceManager.getDefaultSharedPreferences(this);
[...]
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/AdvancedPreferences.html