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 ?

xpy
  • 5,481
  • 3
  • 29
  • 48
Dr.NoBody
  • 1,585
  • 3
  • 12
  • 13
  • @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 <3 – Dr.NoBody Feb 24 '16 at 10:38
  • Also see this tutorial, https://github.com/codepath/android_guides/wiki/Settings-with-PreferenceFragment – K.Sopheak Nov 03 '16 at 03:15

1 Answers1

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.

Zoran P
  • 336
  • 1
  • 9
  • thanks for this code. can you send me how to change `TextView` color with `ListPreference` ? please help me i really need this tutorial. thanks – Dr.NoBody Feb 24 '16 at 10:54
  • What TextView are you refering to? – Zoran P Feb 24 '16 at 10:56
  • I have TextView in MainActivity, and i want change this color from Preference Activity. – Dr.NoBody Feb 24 '16 at 11:03
  • I was three days into this problem,You have to be solved.I am very grateful to you – Dr.NoBody Feb 24 '16 at 11:58
  • 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 Feb 24 '16 at 12:01
  • can you help me for this topic ? : http://stackoverflow.com/questions/35604449/how-to-set-preferencescreen-into-other-layout-in-android – Dr.NoBody Feb 24 '16 at 14:11
  • `findPreference()` is deprecated. – Si8 Oct 29 '16 at 13:19
  • @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 – Zoran P Nov 02 '16 at 11:22
  • Hi, @ZoranP. I want to disable vertical scrollbar when there are a lot items in preferencescreen? I also ask here. https://github.com/codepath/android_guides/issues/227. thank – K.Sopheak Nov 03 '16 at 03:17
  • 1
    @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. – Zoran P Nov 03 '16 at 09:44