0

I´m having trouble with setting preferences in my app.

In the main layout I have the button to open the settings layout:

Button btnPreferences = (Button) findViewById(R.id.btnPreferences);        
    btnPreferences.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent("com.absolutkarlos.AppPreferenceActivity");
           startActivity(i);
        }
    });

Then, it open the PreferenceActivity:

public class AppPreferenceActivity extends PreferenceActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //--load the preferences from an XML file---
    addPreferencesFromResource(R.xml.user_references);

}

}

The xml file:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen 
xmlns:android="http://schemas.android.com/apk/res/android">

<PreferenceCategory android:title="Category 1">
    <CheckBoxPreference
        android:title="Checkbox"
        android:defaultValue="false"
        android:summary="True of False"
        android:key="checkboxPref" />
    </PreferenceCategory>                

<PreferenceCategory android:title="Category 2">
    <EditTextPreference
        android:summary="Enter a string"
        android:defaultValue="@string/food"
        android:title="Edit Text"
        android:key="editTextPref" 
        android:name="@string/name"/>            
    <RingtonePreference
        android:summary="Select a ringtone"
        android:title="Ringtones"
        android:key="ringtonePref" 
        android:name="Ringtone Preference" />            
    <PreferenceScreen                
        android:title="Second Preference Screen"
        android:summary=
            "Click here to go to the second Preference Screen"
        android:key="secondPrefScreenPref" >                            
        <EditTextPreference
            android:summary="Enter a string"
            android:title="Edit Text (second Screen)"
            android:key="secondEditTextPref" 
            android:name="EditText" />                
    </PreferenceScreen>        
</PreferenceCategory>  

</PreferenceScreen>

Everything looks like its going to work ok, but when I test it, don´t save the string I want it to save. I Mean, I click the setting button, open the setting layout, I write a text on the EditTextPreference call NAME, but it doesn´t save it, instead the LogCat: sendUserActionEvet() mView == null So, what I´m doing wrong? Did I miss a step? or forgot to add something?

Basically, I just want to user write his name as part of the settings, this name will be shown at the main layout as a big title. The user can write a nickname if he want. It´s just a string that the app must remember always.

Thanks for your help...

absolutkarlos
  • 570
  • 1
  • 9
  • 22

1 Answers1

0

As you might have read, the message:

LogCat: sendUserActionEvet() mView == null 

can be ignored on S4 devices.

You need read the value of Preference, and change it, for his title, on your AppPreferenceActivity, set onResume method:

    @Override
    public void onResume(){
        super.onResume();
        EditTextPreference preference = (EditTextPreference) findPreference("secondEditTextPref");
        preference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                preference.setTitle(newValue.toString());
                return true;
                }
            });

        String name = preference.getText();
        if(name != null)
            preference.setTitle(name);
    }

Also, you must remove addPreferencesFromResource method, because is deprecated. You should use onBuildHeaders method. Here you can see an example of PreferenceActivity.

Jose Rodriguez
  • 9,753
  • 13
  • 36
  • 52