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
?
From the menu bar, click File > Settings (on macOS, click Android Studio > Preferences).
In Android apps, there are often settings pages that contain different options the user can tweak. The PreferenceFragment and PreferenceFragmentCompat contains a hierarchy of preference objects displayed on screen in a list. These preferences will automatically save to SharedPreferences as the user interacts with them.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With