How to use PreferenceScreen in Android

14,447

Use the following code:

public static class MyPreferenceFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener
{
    @Override
    public void onCreate(final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.setting_prefrences);
    }

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

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

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
    {
        if (key.equals("setting_title_font_color"))
        {
            // get preference by key
            Preference pref = findPreference(key);
            // do your stuff here
        }
    }
}

To change the TextView color in activity you need to add the following code to your activity onCreate():

String color = preferences.getString("setting_title_font_color", "White");
if (color.equals("White") {
    summary_tv.setTextColor(Color.WHITE);
} else if (color.equals("Black") {
    summary_tv.setTextColor(Color.BLACK);
} else {
    // default color
}

Note: the color will be changed only when activity is first created. If you wish to update color while activity is running, then put this code in onResume() method.

Share:
14,447
Dr.NoBody
Author by

Dr.NoBody

Updated on June 04, 2022

Comments

  • Dr.NoBody
    Dr.NoBody about 2 years

    i want use PreferenceScreen for setting page, i know use EditTextPreferences and use this text. but i don't know other Objects, for example : i don't know change text color from ListPreference, or i don't know show/hide text from CheckBoxPreference.

    Attention : Please do not negative. I searched on the internet but could not find an appropriate topic, So here's the question I asked. Please guide me instead of giving a negative rating!

    Main Activity code :

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_page);
    
        ///--- Setting Options
        summary_tv = (TextView) findViewById(R.id.main_summary_text);
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        String colors = "";
        String main_title_text = preferences.getString("setting_title_text", "main_title");
        summary_tv.setText(main_title_text);
        Boolean main_title_show = preferences.getBoolean("setting_title_show", true);
    

    Preference Activity code :

    public class SettingPage extends PreferenceActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
        }
    
        public static class MyPreferenceFragment extends PreferenceFragment
        {
            @Override
            public void onCreate(final Bundle savedInstanceState)
            {
                super.onCreate(savedInstanceState);
                addPreferencesFromResource(R.xml.setting_prefrences);
            }
        }
    }
    

    Preference XML code :

    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    
        <PreferenceCategory
            android:key="setting_title_title_category"
            android:title="Title options">
    
        <CheckBoxPreference
            android:id="@+id/setting_title_show_id"
            android:key="setting_title_show"
            android:title="Show Main Title"
            android:summary="Show/hide MainPage title" />
    
        <EditTextPreference
            android:key="setting_title_text"
            android:title="Set Main Title"
            android:summary="Change MainPage title"
            android:dialogTitle="Change Title"
            android:dialogMessage="Change title please..."/>
    
        </PreferenceCategory>
    
        <PreferenceCategory
            android:key="setting_title_font_category"
            android:title="Font options">
    
            <ListPreference
                android:key="setting_title_font_color"
                android:title="Title font colors"
                android:summary="Change title font colors"
                android:entries="@array/colors"
                android:entryValues="@array/colors"
                android:dialogTitle="Change font color" />
    
        </PreferenceCategory>
    
        <RingtonePreference
            android:title="tes"/>
    
    </PreferenceScreen>
    

    String XML code :

    <resources>
        <string name="app_name">1-MyTestDb Project</string>
        <string name="title_activity_settings">Settings</string>
    
        <string-array name="colors">
    
            <item>White</item>
            <item>Black</item>
            <item>Primary</item>
    
        </string-array>
    
    </resources>
    

    How can use this preferences in java code. for example change TextView color with ListPreference ?

  • Dr.NoBody
    Dr.NoBody over 8 years
    thanks for this code. can you send me how to change TextView color with ListPreference ? please help me i really need this tutorial. thanks
  • Zoran P
    Zoran P over 8 years
    What TextView are you refering to?
  • Dr.NoBody
    Dr.NoBody over 8 years
    I have TextView in MainActivity, and i want change this color from Preference Activity.
  • Dr.NoBody
    Dr.NoBody over 8 years
    I was three days into this problem,You have to be solved.I am very grateful to you
  • Dr.NoBody
    Dr.NoBody over 8 years
    I have one more question in this regard, Here to ask you or I create a new thread and invite you to this thread?
  • Dr.NoBody
    Dr.NoBody over 8 years
    can you help me for this topic ? : stackoverflow.com/questions/35604449/…
  • Si8
    Si8 over 7 years
    findPreference() is deprecated.
  • Zoran P
    Zoran P over 7 years
    @Si8 it is deprecated only in PreferenceActivity since from API 11 it is encouraged to use PreferenceFragment. Since my original post uses PreferenceFragment, your comment is not rendered moot
  • K.Sopheak
    K.Sopheak over 7 years
    Hi, @ZoranP. I want to disable vertical scrollbar when there are a lot items in preferencescreen? I also ask here. github.com/codepath/android_guides/issues/227. thank
  • Zoran P
    Zoran P over 7 years
    @K.Sopheak Since I don't know how you implement your preferences, I cannot give you a precise answer. I can give you an example. I use PreferenceActivity with PreferenceFragment for each preference screen. My PreferenceActivity defines it's own layout with ListView and programatically sets it with setContentView(R.layout.preferences_activity). You can define android:scrollbars="none" in ListView element in preference activity layout. It will be applied to all preference fragments as well.