I'm working with Preferences in android and I have a problem, I have a fragment that is a PreferenceFragment, in that fragment I have several options one of them when I click it opens a dialogue box with a field to place text inside as shown in the following image:
Clicking on the option to change user shows the following:
I want to know how I can recover the text that you enter in that EditTextPreference to later save it as a new login name: the code is as follows:
This is the fragment layout:
<PreferenceCategory>
<EditTextPreference
android:key="opcion1"
android:summary="Da clic aqui para cambiar el nombre de usuario que fue registrado."
android:title="Cambiar usuario" />
</PreferenceCategory>
<PreferenceCategory>
<EditTextPreference
android:key="opcion2"
android:summary="Da clic aqui para cambiar el nombre de usuario que fue registrado."
android:title="Cambiar contraseña" />
</PreferenceCategory>
<PreferenceCategory>
<SwitchPreference
android:key="opcion3"
android:summary="Deseas rescibir notificaciones?"
android:title="Notificaciones" />
</PreferenceCategory>
and this is the code of the fragment :
package com.example.enriq.persistencia_en_android_enrique_espinosa;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.widget.TextView;
import android.widget.Toast;
public class ConfiguracionesFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.configuraciones);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
String nomreusuario = preferences.getString("opcion1", "");
if(nomreusuario != null){
Toast.makeText(getActivity(),"El nuevo nombre de usuario es: "+nomreusuario, Toast.LENGTH_SHORT).show();
}
}
}