1. Are you ready for the Galaxy S20? Here is everything we know so far!

Showing set preference in summary

Discussion in 'Android Development' started by Change, Jan 19, 2016.

  1. Change

    Change Lurker
    Thread Starter

    Hi all,

    I have managed to create way to set preferences (using this tutorial/framework: http://www.cs.dartmouth.edu/~campbell/cs65/lecture12/lecture12.html

    But my question is how would I be able to show the set preference as summary text?

    Code (Text):
    1. <EditTextPreference
    2.                 android:key="edittext_preference"
    3.                 android:title="@string/title_edittext_preference"
    4.                 android:summary="CURRENT SET PREFERENCE"
    5.                 android:dialogTitle="@string/dialog_title_edittext_preference" />
    And am I correct in understanding that:
    Code (Text):
    1.  
    2. String randomString = getPreferences(MODE_PRIVATE).getString("edittext_preference","default");
    3.  
    Will get the textpreference set above?

    Thanks in advance,
    Change


    P.s. Yes I am new to coding
     



    1. Download the Forums for Android™ app!


      Download

       
  2. Deleted User

    Deleted User Guest

    Looks good to me. :thumbsupdroid:
     
  3. Change

    Change Lurker
    Thread Starter

    Got it working:
    Code (Text):
    1.  
    2. @Override
    3. public void onResume(){
    4.     super.onResume();
    5.     // Set up a listener whenever a key changes
    6.     getPreferenceScreen().getSharedPreferences()
    7.             .registerOnSharedPreferenceChangeListener(this);
    8.     updatePreference(KEY_EDIT_TEXT_PREFERENCE);
    9. }
    10.  
    11. @Override
    12. public void onPause() {
    13.     super.onPause();
    14.     // Unregister the listener whenever a key changes
    15.     getPreferenceScreen().getSharedPreferences()
    16.             .unregisterOnSharedPreferenceChangeListener(this);
    17. }
    18.  
    19. @Override
    20. public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
    21.                                       String key) {
    22.     updatePreference(key);
    23. }
    24.  
    25. private void updatePreference(String key){
    26.     if (key.equals(KEY_EDIT_TEXT_PREFERENCE)){
    27.         Preference preference = findPreference(key);
    28.         if (preference instanceof EditTextPreference){
    29.             EditTextPreference editTextPreference =  (EditTextPreference)preference;
    30.             if (editTextPreference.getText().trim().length() > 0){
    31.                 editTextPreference.setSummary(editTextPreference.getText());
    32.             }else{
    33.                 editTextPreference.setSummary("Enter Your Name");
    34.             }
    35.         }
    However I do not really understand the code, can someone be so kind as to explain how exactly this works?

    -Change
     
  4. Deleted User

    Deleted User Guest

    What part don't you understand?
     
  5. Change

    Change Lurker
    Thread Starter

    I get the updatePreference() part, but I do not know what the super.onResume and onPause do .
    Code (Text):
    1.  
    2. @Override
    3. public void onResume(){
    4.     super.onResume();
    5.     // Set up a listener whenever a key changes
    6.     getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    7.   }
    8.  
    9. @Override
    10. public void onPause() {
    11.     super.onPause();
    12.     // Unregister the listener whenever a key changes
    13.     getPreferenceScreen().getSharedPreferences()
    14.             .unregisterOnSharedPreferenceChangeListener(this);
    15. }
    16.  
    17. @Override
    18. public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    19.     updatePreference(key);
    20. }

    Thanks in advance,
    Change
     
  6. Deleted User

    Deleted User Guest

    Change likes this.
  7. Change

    Change Lurker
    Thread Starter

Loading...
Similar Threads - Showing set preference
  1. lynxstch
    Replies:
    4
    Views:
    380
  2. King Mustard
    Replies:
    2
    Views:
    429
  3. ang3l1983
    Replies:
    1
    Views:
    390
  4. kite
    Replies:
    4
    Views:
    556
  5. lthygesen
    Replies:
    7
    Views:
    863
  6. Berkin Tasdemir
    Replies:
    0
    Views:
    692
  7. _wigfield_
    Replies:
    8
    Views:
    1,393
  8. mitcheca
    Replies:
    4
    Views:
    671
  9. icantchooseone
    Replies:
    2
    Views:
    1,192
  10. dnagene20
    Replies:
    2
    Views:
    2,600

Share This Page

Loading...