[android-developers] Re: How to force hierarchy change to PreferenceActivity?
Thanks for helpful comment, Jason. I got an answer for this. I pasted for those who want to do similar. - protected void onCreate(Bundle savedInstanceState) { createPreferenceHierarchy(); setPreferenceScreen(mRoot); } private void createPreferenceHierarchy() { // Root mRoot = getPreferenceManager().createPreferenceScreen(this); // Orientation tracking smoothing method preference mOrientationSmoothingPref = getPreferenceManager ().createPreferenceScreen(this); mOrientationSmoothingPref.setKey( (String) getResources().getText (R.string.key_screen_orientation_smoothing_method) ); mOrientationSmoothingPref.setTitle (R.string.title_screen_orientation_smoothing_method); mOrientationSmoothingPref.setSummary (R.string.summary_screen_orientation_smoothing_method); mRoot.addPreference(mOrientationSmoothingPref); // List preference mOrientationSmoothinglistPref = new ListPreference(this); mOrientationSmoothinglistPref.setEntries (R.array.entries_orientation_smoothing_method_preference); mOrientationSmoothinglistPref.setEntryValues (R.array.entryvalues_orientation_smoothing_method_preference); mOrientationSmoothinglistPref.setKey( (String) getResources ().getText(R.string.key_preference_orientation_smoothing_method) ); mOrientationSmoothinglistPref.setTitle (R.string.title_preference_orientation_smoothing_method); mOrientationSmoothinglistPref.setDialogTitle (R.string.title_preference_orientation_smoothing_method); mOrientationSmoothinglistPref.setDefaultValue("No"); mOrientationSmoothingPref.addPreference (mOrientationSmoothinglistPref); // Create parameter preference category createOrientationSmoothingParameterPreferenceCategory(); } private void createOrientationSmoothingParameterPreferenceCategory() { mOriantationSmoothingParameterPrefCat = new PreferenceCategory(this); if ( mOrientationSmoothinglistPref.getValue().equals("ma") ) { // Set summary to list pref mOrientationSmoothinglistPref.setSummary("Moving Averaging"); // Moving averaging parameters category mOriantationSmoothingParameterPrefCat.setTitle (R.string.title_category_orientation_ma_parameter); mOrientationSmoothingPref.addPreference (mOriantationSmoothingParameterPrefCat); // Edit text preference mMaNPref = new EditTextPreference(this); mMaNPref.setKey( (String) getResources().getText (R.string.key_preference_ma_n) ); mMaNPref.setDialogTitle(R.string.title_preference_ma_n); mMaNPref.setTitle(R.string.title_preference_ma_n); mMaNPref.setSummary(R.string.summary_preference_ma_n); mMaNPref.setDefaultValue("10"); mOriantationSmoothingParameterPrefCat.addPreference(mMaNPref); } else if ( mOrientationSmoothinglistPref.getValue().equals("ses") ) { // Set summary to list pref mOrientationSmoothinglistPref.setSummary("Single exponential smoothing"); // Single exponential smoothing parameters category mOriantationSmoothingParameterPrefCat.setTitle (R.string.title_category_orientation_ses_parameter); mOrientationSmoothingPref.addPreference (mOriantationSmoothingParameterPrefCat); // Edit text preference mSesalphaPref = new EditTextPreference(this); mSesalphaPref.setKey( (String) getResources().getText (R.string.key_preference_ses_alpha) ); mSesalphaPref.setTitle(R.string.title_preference_ses_alpha); mSesalphaPref.setDialogTitle (R.string.title_preference_ses_alpha); mSesalphaPref.setSummary (R.string.summary_preference_ses_alpha); mSesalphaPref.setDefaultValue("0.3"); mOriantationSmoothingParameterPrefCat.addPreference (mSesalphaPref); } else if ( mOrientationSmoothinglistPref.getValue().equals("des") ) { // Set summary to list pref mOrientationSmoothinglistPref.setSummary("Double exponential smoothing"); // Double exponential smoothing parameters category mOriantationSmoothingParameterPrefCat.setTitle (R.string.title_category_orientation_des_parameter); mOrientationSmoothingPref.addPreference (mOriantationSmoothingParameterPrefCat); // Edit text preference mDesalphaPref = new EditTextPreference(this); mDesalphaPref.setKey( (String) getResources().getText (R.string.key_preference_des_alpha) ); mDesalphaPref.setTitle(R.string.title_preference_des_alpha); mDesalphaPref.setDialogTitle (R.string.title_preference_des_alpha); mDesalphaPref.setSummary (R.string.summary_preference_des_alpha); mDesalphaPref.setDefaultValue("0.3"); mOriantationSmoothingParameterPrefCat.addPreference (mDesalphaPref); // Edit text preference mDesgammaPref = new EditTextPreference(this); mDesgammaPref.setKey( (String) getResources().getText (R.string.key_preference_des_gamma) ); mDesgammaPref.setTitle(R.string.title_preference_des_gamma); mDesgammaPref.setDialogTi
[android-developers] Re: How to force hierarchy change to PreferenceActivity?
The preference framework will refresh the UI when a preference has a UI-visible attribute changed, for example the summary. So, you can just do myPreference.setSummary(myPreference.getValue()) (assuming you want to show the selected item as the summary of the preference). On Fri, Jun 12, 2009 at 4:29 AM, yoshitaka tokusho wrote: > > I have a PreferenceActivity building screen hierarchy from code, and I > need to force display hierarchy change dynamically when one of > SharedPreference value changed. I know this might be a kind of easy > quesiton, but I couldn't find answer anywhere in documentations or > discussion. > > My PreferenceActivity is forming like this. > > - > public class MyPreferenceActivity extends PreferenceActivity > ... > setPreferenceScreen(createPreferenceHierarchy()); > } > > private PreferenceScreen createPreferenceHierarchy() > { >// Root >PreferenceScreen root = getPreferenceManager > ().createPreferenceScreen(this); > >// Orientation tracking smoothing method preference >PreferenceScreen orientationsmoothingPref = getPreferenceManager > ().createPreferenceScreen(this); >orientationsmoothingPref.setKey( (String) getResources().getText > (R.string.key_screen_orientation_smoothing_method) ); >orientationsmoothingPref.setTitle > (R.string.title_screen_orientation_smoothing_method); >orientationsmoothingPref.setSummary > (R.string.summary_screen_orientation_smoothing_method); >root.addPreference(orientationsmoothingPref); > >// List preference >ListPreference orientationsmoothinglistPref = new ListPreference > (this); >orientationsmoothinglistPref.setEntries > (R.array.entries_orientation_smoothing_method_preference); >orientationsmoothinglistPref.setEntryValues > (R.array.entryvalues_orientation_smoothing_method_preference); >orientationsmoothinglistPref.setKey( (String) getResources().getText > (R.string.key_preference_orientation_smoothing_method) ); >orientationsmoothinglistPref.setTitle > (R.string.title_preference_orientation_smoothing_method); >orientationsmoothinglistPref.setSummary > (R.string.summary_preference_orientation_smoothing_method); >orientationsmoothinglistPref.setDefaultValue("No"); > > orientationsmoothingPref.addPreference(orientationsmoothinglistPref); > >// Orientation smoothing parameters category >PreferenceCategory orientationsmoothingparameterPrefCat = new > PreferenceCategory(this); >orientationsmoothingparameterPrefCat.setTitle > (R.string.title_category_orientation_smoothing_method_parameter); >orientationsmoothingPref.addPreference > (orientationsmoothingparameterPrefCat); > >String orientationsmoothingmethodStr = > orientationsmoothinglistPref.getValue(); > >if ( orientationsmoothingmethodStr.compareTo("no") == 0 ) >{ >/* In case orientation smoothing method is "No" */ >// Nothing to do. >} >else if ( orientationsmoothingmethodStr.compareTo("ma") == 0 ) >{ >/* In case orientation smoothing method is "Moving > Averaging" */ > >// Edit text preference >EditTextPreference maNPref = new EditTextPreference(this); >maNPref.setKey( (String) getResources().getText > (R.string.key_preference_ma_n) ); >maNPref.setDialogTitle(R.string.title_preference_ma_n); >maNPref.setTitle(R.string.title_preference_ma_n); >maNPref.setSummary(R.string.summary_preference_ma_n); >maNPref.setDefaultValue("10"); >orientationsmoothingparameterPrefCat.addPreference(maNPref); > >} >else if ( orientationsmoothingmethodStr.compareTo("ses") == 0 ) >{ >/* In case orientation smoothing method is "Single > Exponential > Smoothing" */ > >// Edit text preference >EditTextPreference sesalphaPref = new EditTextPreference(this); >sesalphaPref.setKey( (String) getResources().getText > (R.string.key_preference_ses_alpha) ); >sesalphaPref.setTitle(R.string.title_preference_ses_alpha); > > sesalphaPref.setDialogTitle(R.string.title_preference_ses_alpha); >sesalphaPref.setSummary(R.string.summary_preference_ses_alpha); >sesalphaPref.setDefaultValue("0.3"); > > orientationsmoothingparameterPrefCat.addPreference(sesalphaPref); > >} >else if ( orientationsmoothingmethodStr.compareTo("des") == 0 ) >{ >/* In case orientation smoothing method is "Single > Exponential > Smoothing" */ > >// Edit text preference >EditTextPreference desalphaPref = new EditTextPreference(this); >desalphaPref.setKey( (String) getResources().getText > (R.string.key_preference_des_alpha) ); >desalphaPref.setTitle(R.string.title_preference_des_alpha); > > desalphaPref.setDial