How to use PreferenceScreen in Android
Asked Answered
N

1

11

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 ?

Nanete answered 24/2, 2016 at 10:10 Comment(2)
@cricket_007, yes i look this tutorial and i know use EditTextPreference, but i can't use CheckboxPreference, ListPreference ! can you send me how to use this? for example change TextView color with ListPreference ? Thanks <3Nanete
Also see this tutorial, github.com/codepath/android_guides/wiki/…Klystron
S
8

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.

Selfappointed answered 24/2, 2016 at 10:37 Comment(10)
thanks for this code. can you send me how to change TextView color with ListPreference ? please help me i really need this tutorial. thanksNanete
What TextView are you refering to?Selfappointed
I have TextView in MainActivity, and i want change this color from Preference Activity.Nanete
I was three days into this problem,You have to be solved.I am very grateful to youNanete
I have one more question in this regard, Here to ask you or I create a new thread and invite you to this thread?Nanete
can you help me for this topic ? : #35604949Nanete
findPreference() is deprecated.Pyretic
@Pyretic 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 mootSelfappointed
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. thankKlystron
@Klystron 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.Selfappointed

© 2022 - 2024 — McMap. All rights reserved.