Android + Preferences
<p>I have a <code>preferences.xml</code> and <code>Settings.java</code>... In the <code>onCreate</code> method I want to set the summary of the <code>preferences</code>... Could someone guide me what is the best way to do it???
What I know is that one way of doing is</p>

<pre><code>activityName_text = (EditTextPreference) findPreference("activityName");
    activityName_text.setSummary(activityName_text.getText());
</code></pre>

<p>but for each key if I follow the above pattern I feel that the code will be bad...  </p>

<p><strong>Preferences.xml</strong>:
    
    </p>

<pre><code>    &lt;PreferenceScreen android:title="Header" &gt;
        &lt;EditTextPreference
            android:defaultValue="TestRequest"
            android:key="activityName"
            android:title="activityName" /&gt;
        &lt;EditTextPreference
            android:defaultValue="Request"
            android:key="msgName"
            android:title="msgName" /&gt;

        &lt;ListPreference
            android:defaultValue="0"
            android:entries="@array/msg_type"
            android:entryValues="@array/msg_type_values"
            android:key="msgType"
            android:title="msgType" /&gt;

        &lt;EditTextPreference
            android:defaultValue="AA"
            android:key="senderURI"
            android:title="senderURI" /&gt;
        &lt;EditTextPreference
            android:defaultValue="BB"
            android:key="destinationURI"
            android:title="destinationURI" /&gt;
        &lt;EditTextPreference
            android:defaultValue="AA"
            android:key="replyToURI"
            android:title="replyToURI" /&gt;
        &lt;EditTextPreference
            android:defaultValue="BB"
            android:key="originatorURI"
            android:title="originatorURI" /&gt;
        &lt;EditTextPreference
            android:defaultValue="FF"
            android:key="failureReplytoURI"
            android:title="failureReplytoURI" /&gt;
        &lt;EditTextPreference
            android:defaultValue="AA"
            android:key="correlationId"
            android:title="correlationId" /&gt;
        &lt;EditTextPreference
            android:defaultValue="HH"
            android:key="security"
            android:title="security" /&gt;
        &lt;EditTextPreference
            android:defaultValue="GG"
            android:key="securityType"
            android:title="securityType" /&gt;
        &lt;EditTextPreference
            android:defaultValue="HH"
            android:key="priority"
            android:title="priority" /&gt;

        &lt;ListPreference
            android:defaultValue="0"
            android:entries="@array/communicationPattern"
            android:entryValues="@array/communicationPatternValues"
            android:key="communicationPattern"
            android:title="communicationPattern" /&gt;
        &lt;ListPreference
            android:defaultValue="0"
            android:entries="@array/communicationStyle"
            android:entryValues="@array/communicationStyleValues"
            android:key="communicationStyle"
            android:title="communicationStyle" /&gt;


        &lt;EditTextPreference
            android:defaultValue="100"
            android:digits="0123456789"
            android:key="requestedBatchSize"
            android:title="requestedBatchSize" /&gt;
        &lt;!-- radhika-number format --&gt;
        &lt;EditTextPreference
            android:defaultValue="78"
            android:digits="0123456789"
            android:key="batchSequenceNumber"
            android:title="batchSequenceNumber" /&gt;

        &lt;ListPreference
            android:defaultValue="0"
            android:entries="@array/batchSequenceEndOfReply"
            android:entryValues="@array/batchSequenceEndOfReplyValues"
            android:key="batchSequenceEndOfReply"
            android:title="batchSequenceEndOfReply" /&gt;

        &lt;EditTextPreference
            android:defaultValue="HH"
            android:key="iteratorReferenceURI"
            android:title="iteratorReferenceURI" /&gt;
        &lt;EditTextPreference
            android:defaultValue="KK"
            android:key="fileLocationURI"
            android:title="fileLocationURI" /&gt;
        &lt;EditTextPreference
            android:defaultValue="10/10/2010"
            android:key="timestamp"
            android:title="timestamp" /&gt;
        &lt;EditTextPreference
            android:defaultValue="kkk"
            android:key="vendorExtensions"
            android:title="vendorExtensions" /&gt;

        &lt;PreferenceCategory
            android:key="activityStatus"
            android:title="activityStatus" &gt;
            &lt;ListPreference
                android:defaultValue="0"
                android:entries="@array/activityStatusType"
                android:entryValues="@array/activityStatusTypeValues"
                android:key="actStatus"
                android:title="activityStatus" /&gt;

            &lt;EditTextPreference
                android:defaultValue="AA"
                android:key="qualifier"
                android:title="qualifier" /&gt;
        &lt;/PreferenceCategory&gt;


        &lt;PreferenceCategory
            android:key="msgSpecificProperties"
            android:title="msgSpecificProperties" &gt;
            &lt;EditTextPreference
                android:defaultValue="multiple values"
                android:key="propName"
                android:title="propName" /&gt;
            &lt;EditTextPreference
                android:defaultValue="multiple values"
                android:key="propValue"
                android:title="propValue" /&gt;
        &lt;/PreferenceCategory&gt;
        &lt;PreferenceCategory
            android:key="compressionType"
            android:title="compressionType" &gt;
            &lt;EditTextPreference
                android:defaultValue="kkk"
                android:key="compressionTypeAttr"
                android:title="extension" /&gt;

            &lt;ListPreference
                android:defaultValue="0"
                android:entries="@array/compressionType"
                android:entryValues="@array/compressionTypeValues"
                android:key="compressionTypeValue"
                android:title="value" /&gt;
        &lt;/PreferenceCategory&gt;
        &lt;PreferenceCategory
            android:key="packingType"
            android:title="packingType" &gt;
            &lt;EditTextPreference
                android:defaultValue="kkk"
                android:key="packingTypeAttr"
                android:title="extension" /&gt;

            &lt;ListPreference
                android:defaultValue="0"
                android:entries="@array/packingType"
                android:entryValues="@array/packingTypeValues"
                android:key="packingTypeValue"
                android:title="value" /&gt;
        &lt;/PreferenceCategory&gt;
    &lt;/PreferenceScreen&gt;
    &lt;PreferenceScreen android:title="Body" &gt;
        &lt;PreferenceScreen android:title="Managed Elements" &gt;
            &lt;PreferenceCategory
                android:key="mdOrMlsnRef"
                android:title="md Or Mlsn Ref" &gt;
                &lt;EditTextPreference
                    android:defaultValue="Name"
                    android:key="rdnName"
                    android:title="rdnName" /&gt;
                &lt;EditTextPreference
                    android:defaultValue="Value"
                    android:key="rdnValue"
                    android:title="rdnValue" /&gt;
            &lt;/PreferenceCategory&gt;
        &lt;/PreferenceScreen&gt;
        &lt;PreferenceScreen android:title="Active Alarms" &gt;
        &lt;/PreferenceScreen&gt;
    &lt;/PreferenceScreen&gt;
&lt;/PreferenceScreen&gt;
</code></pre>

<p><strong>Settings.java:</strong></p>

<pre><code>package com.example.settingstest;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;


public class Settings extends PreferenceActivity implements OnSharedPreferenceChangeListener {

    EditTextPreference activityName_text;
    EditTextPreference msgName_text;

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
        addPreferencesFromResource(R.xml.preferences);  
    }

    @Override
    protected void onResume() {
        super.onResume();       
        getPreferenceScreen().getSharedPreferences()
        .registerOnSharedPreferenceChangeListener(this);
    }

    @Override protected void onPause() {
        super.onPause();        
        getPreferenceScreen().getSharedPreferences()
        .unregisterOnSharedPreferenceChangeListener(this); 
    }   


    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

        Preference p = findPreference(key);
        if(p instanceof EditTextPreference)
        {           
            p.setSummary(sharedPreferences.getString(key, (String)p.getSummary()));
        }
        else if(p instanceof ListPreference)
        {           
            p.setSummary((String)((ListPreference) p).getEntry());
        }       
    }
}
</code></pre>
