0

In my App, I want to show a Dialog, where the user has to fill in an email address.

When OK is pressed, this entered address should be set as a summary in my PreferenceActivity.

(I want to change the summary of a Preference from another (myDialog) Activity.)

Edit:

builder.setPositiveButton(R.string.okay), new DialogInterface.OnClickListener() {  
  @Override  
  public void onClick(DialogInterface dialog, int which) {  
      if(Utilities.isValidEmail(userEmail.getText())) {  
          //don't know how to access preference and save text as summary
      }
  }
}

Solution:

When OK from Dialog was pressed:

PreferenceManager.getDefaultSharedPreferences(context).edit().putString("email", enteredMail).commit();

When the Settings onCreate method is called:

String mail = sharedPreferences.getString("email", "no entered Mail");
Preference eMail = findPreference("preference_key");
eMail.setSummary(mail);
Thomas
  • 9
  • 3

1 Answers1

0

Your question doesnt make a whole deal of sense. Are you in a preference screen?

use

 <EditTextPreference
        android:defaultValue=""
        android:key="MYEMAILADDRESS"
        android:summary="Email address of user"
        android:title="Email" />

This will present the user with a styled (of their OS) input/dialog for their email. Which will then save to sharedpreferences when ok is clicked.

More on preference screens

IAmGroot
  • 13,760
  • 18
  • 84
  • 154
  • I don't want, that the user have the possibility to change the summary, by pressing the Preference item. When my App starts and the _Main Activity_ is created I check if there was already entered an email, if not, the dialog is shown and then the address should be set as a summary in my PreferenceActivity. – Thomas Oct 07 '14 at 12:22
  • @Thomas I dont know what you mean by "set as a summary". I assume you are still saving to sharedpreferences. Just check if it exists as a key, else put up a dialog. What part are you struggling on.. What have you tried. – IAmGroot Oct 07 '14 at 12:46
  • I want to set the email as the summary text of a preference item. My Problem is, that I don't know, what I have to write in the _onClick_ method in my Dialog class, so that the entered address is set as the summary of a preference. – Thomas Oct 07 '14 at 13:09
  • @Thomas In your `Onclick`, access the text box and get its value. Then access the preference, and set the value. Its hard to tell you more, as you have provided no code. And I am working again, so have no time. – IAmGroot Oct 07 '14 at 13:21
  • Thanks, but I don't know how to access the preference there and how to save that change. I have added some code above. – Thomas Oct 07 '14 at 13:45
  • @Thomas Have a look here on how to dynamically set the summary http://stackoverflow.com/questions/531427/how-do-i-display-the-current-value-of-an-android-preference-in-the-preference-su?rq=1 – IAmGroot Oct 07 '14 at 13:53
  • I've read it, but I don't find a solution which could help me. I don't know, how to get the Preference, change the summary and save it at the end. – Thomas Oct 07 '14 at 15:07
  • @Thomas The top solution does exactly that... It finds the preference, changes the summary which is done instantly and doesnt need saving. This however needs to be done from the preference screen. So the only issue you have is to save the email, then load it on the preference screen and call the linked code. Storing and retreieving strings can be found here. http://stackoverflow.com/a/12074219/940834 If you gave more code, like your oncreate method in preferenceactivity, i could have given an example – IAmGroot Oct 07 '14 at 15:26
  • It works now. With your second link (http://stackoverflow.com/a/12074219/940834), I understood how I have to do this. I've posted my solution above. Thanks a lot! – Thomas Oct 07 '14 at 15:56
  • @Thomas Cool. That's exactly what I meant. Good job :) – IAmGroot Oct 07 '14 at 17:53