Display Summary on Preference Screen Android - android

I have a PreferenceScreen that has a PreferenceScreen within it. I am able to display a summary in my outer most PreferenceScreen on launch and also update the summaries onSharedPreferenceChanged, but my inner PreferenceScreen (in xml it has string #string/advanced) does not get the initial value...it does update onSharedPreferenceChanged. I need the intial to display as well. Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="main_pref">
<PreferenceCategory
android:title="#string/location_and_notifications">
<ListPreference
android:key="pref_temp_notifications"
android:title="#string/notifications"
android:entries="#array/pref_temp_notifications"
android:entryValues="#array/pref_temp_notifications_values"
android:dialogTitle="#string/notifications"
android:defaultValue="2"/>
<ListPreference
android:key="pref_notification_interval"
android:title="#string/notification_interval"
android:entries="#array/pref_notification_interval"
android:entryValues="#array/pref_notification_interval_values"
android:dialogTitle="#string/notification_interval"
android:defaultValue="15" />
<ListPreference
android:key="pref_open_at_launch"
android:title="#string/open_at_launch"
android:entries="#array/pref_open_at_launch"
android:entryValues="#array/pref_open_at_launch_values"
android:dialogTitle="#string/open_at_launch"
android:defaultValue="1"/>
<ListPreference
android:key="pref_push_notification"
android:title="#string/push_enabled"
android:entries="#array/pref_push_notification"
android:entryValues="#array/pref_push_notification_values"
android:dialogTitle="#string/push_enabled"
android:defaultValue="1"/>
</PreferenceCategory>
<PreferenceCategory
android:title="#string/units">
<ListPreference
android:key="pref_temp_units"
android:title="#string/temperature"
android:defaultValue="#string/default_metric"
android:entries="#array/pref_temp_units"
android:entryValues="#array/pref_temp_units_values"
android:dialogTitle="#string/units" />
<PreferenceScreen
android:title="#string/advanced"
android:key="advanced_pref">
<ListPreference
android:key="pref_speed"
android:title="#string/speed"
android:entries="#array/pref_speed"
android:entryValues="#array/pref_speed_values"
android:defaultValue="#string/default_metric"/>
<ListPreference
android:key="pref_measurement"
android:title="#string/measurement"
android:entries="#array/pref_measurement"
android:entryValues="#array/pref_measurement_values"
android:defaultValue="#string/default_metric"/>
<ListPreference
android:key="pref_time"
android:title="#string/time_format"
android:entries="#array/pref_time"
android:entryValues="#array/pref_time_values"
android:defaultValue="#string/default_metric"/>
<ListPreference
android:key="pref_date"
android:title="#string/date_format"
android:entries="#array/pref_date"
android:entryValues="#array/pref_date_values"
android:defaultValue="#string/default_metric"/>
</PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory
android:title="#string/personalization">
<ListPreference
android:key="pref_color_theme"
android:title="#string/color_theme"
android:entries="#array/pref_color_theme"
android:entryValues="#array/pref_color_theme_values"
android:dialogTitle="#string/color_theme"
android:defaultValue="-10981143" />
</PreferenceCategory>
<PreferenceCategory
android:title="#string/more">
<Preference
android:title="#string/platinum"
android:summary="#string/disable_ads"
android:key="upgradePref"/>
<Preference
android:title="#string/rate_update"
android:summary="#string/rate_app"
android:key="ratePref"/>
</PreferenceCategory>
<PreferenceCategory
android:title="#string/about">
<Preference
android:title="#string/eula"
android:summary=""
android:key="eulaPref"/>
<Preference
android:title="#string/privacy_policy"
android:summary=""
android:key="privacyPolicyPref"/>
<PreferenceScreen
android:title="#string/version"
android:summary=""
android:key="version">
</PreferenceScreen>
<PreferenceScreen
android:title="#string/customer_support"
android:summary="#string/email_us">
<intent android:action="com.accuweather.android.EMAIL_ACCUWX"
/>
</PreferenceScreen>
<PreferenceScreen
android:title="#string/accuweather_branding"
android:summary=""
android:key="accuweatherBranding">
</PreferenceScreen>
</PreferenceCategory>
</PreferenceScreen>
Here is a code snippet:
in OnCreate I call this:
for(int i=0;i<getPreferenceScreen().getPreferenceCount();i++){
initSummary(getPreferenceScreen().getPreference(i));
}
private void initSummary(Preference p) {
if (p instanceof PreferenceCategory){
PreferenceCategory pCat = (PreferenceCategory)p;
for(int i=0;i<pCat.getPreferenceCount();i++){
initSummary(pCat.getPreference(i));
}
}else{
updatePrefSummary(p);
}
}
private void updatePrefSummary(Preference p) {
if (p instanceof ListPreference) {
ListPreference listPref = (ListPreference) p;
p.setSummary(listPref.getEntry());
}
}
Then in onSharedPreferenceChanged:
#Override
public void onSharedPreferenceChanged(SharedPreferences sp, String key) {
Log.i(DEBUG_TAG, "value of key is " + key);
Preference pref = findPreference(key);
...
updatePrefSummary(findPreference(key));
}

First, I have these constants in another class to reference:
public static final String PREF_TIME = "pref_time";
public static final String PREF_DATE = "pref_date";
public static final String PREF_SPEED = "pref_speed";
public static final String PREF_MEASUREMENT = "pref_measurement";
In order to get access to the embedded PreferenceScreen within the Preference Screen I do this:
// enable summary of one deep preferenceScreen
Preference advancedSpeedPref = (Preference) findPreference(ACCUWX.Preferences.PREF_SPEED);
ListPreference listPref1 = (ListPreference) advancedSpeedPref;
advancedSpeedPref.setSummary(listPref1.getEntry());
Preference advancedMeasurementPref = (Preference) findPreference(ACCUWX.Preferences.PREF_MEASUREMENT);
ListPreference listPref2 = (ListPreference) advancedMeasurementPref;
advancedMeasurementPref.setSummary(listPref2.getEntry());
Preference advancedTimeformatPref = (Preference) findPreference(ACCUWX.Preferences.PREF_TIME);
ListPreference listPref3 = (ListPreference) advancedTimeformatPref;
advancedTimeformatPref.setSummary(listPref3.getEntry());
Preference advancedDateformatPref = (Preference) findPreference(ACCUWX.Preferences.PREF_DATE);
ListPreference listPref4 = (ListPreference) advancedDateformatPref;
advancedDateformatPref.setSummary(listPref4.getEntry());
Works like a charm!

Related

How to increase text size of Preference Category

I want to increase size of preference categaory title programmatically so how can do this ?
below is my xml
<PreferenceCategory android:title="#string/languagetitle"
android:key="LanguageCategory">
<ListPreference
android:defaultValue="#string/defaultlanguagevalue"
android:entries="#array/language"
android:entryValues="#array/languagevalues"
android:key="language"
android:summary="#string/languageSubtitle"
android:title="English" /></PreferenceCategory>
<PreferenceCategory android:title="#string/Notificationtitle"
android:key="NotificationCategory">
<SwitchPreference
android:defaultValue="true"
android:key="notification"
android:summary="#string/NotificationSubtitle"
android:title="#string/Notificationcontent" />
<ListPreference
android:entries="#array/time"
android:entryValues="#array/time"
android:key="time"
android:summary="#string/NotificationSubtitleTime"
android:title="#string/NotificationcontentTime"
android:defaultValue="9am"
/>
</PreferenceCategory>
<PreferenceCategory
android:title="#string/FontTitle"
android:key="FontStyleCategory"
>
<faisal.home.com.tafaqquhfiddin.FontStylePreference
android:key="fontstyle"
/>
<faisal.home.com.tafaqquhfiddin.FontSizePreference
android:key="fontsize"
/>
</PreferenceCategory>
<PreferenceCategory
android:title="#string/OTHERS"
android:key="FontSizeCategory">
<faisal.home.com.tafaqquhfiddin.About_us_Preference/>
</PreferenceCategory>
</PreferenceScreen>
and below is my code where i want to increase its size programmatically
public void OnLanguageChange()
{
SharedPreferences sharedPreferences= PreferenceManager.getDefaultSharedPreferences(getActivity());
PreferenceCategory languageCategory =(PreferenceCategory) findPreference("LanguageCategory");
Preference language =findPreference(MyAppManager.LANGUAGE_KEY);
PreferenceCategory notificationCategory =(PreferenceCategory) findPreference("NotificationCategory");
Preference notification =findPreference(MyAppManager.NOTIFICATION_KEY);
Preference notificationTime =findPreference(MyAppManager.NOTIFICATION_TIME_KEY);
PreferenceCategory fontStyleCategory=(PreferenceCategory)findPreference("FontStyleCategory");
PreferenceCategory fontSizeCategory=(PreferenceCategory)findPreference("FontSizeCategory");
languageCategory.setTitle(getString(R.string.languagetitle));
language.setSummary(getString(R.string.languageSubtitle));
notificationCategory.setTitle(getString(R.string.Notificationtitle));
notification.setTitle(getString(R.string.Notificationcontent));
notification.setSummary(getString(R.string.NotificationSubtitle));
notificationTime.setTitle(getString(R.string.NotificationcontentTime));
notificationTime.setSummary(getString(R.string.NotificationSubtitleTime)+" "+ sharedPreferences.getString(MyAppManager.NOTIFICATION_TIME_KEY, "9am"));
fontStyleCategory.setTitle(getString(R.string.FontTitle));
fontSizeCategory.setTitle(getString(R.string.FontSize));
}
I am new to android. Please help me how can i achieve this

How to combine preference and CardView

I am trying to make a setting menu with cardView and Preference, but I am not sure how to combine these two... Here's the screenshot and I want to move the ListPreference and PreferenceScreen entries into the setting cardView above.
Here's the preference.xml The layout/setting_group is a cardview
<PreferenceScreen
android:layout="#layout/setting_title_top"
android:title = "Padding" />
<PreferenceScreen
android:layout="#layout/setting_group">
</PreferenceScreen>
<ListPreference
android:id="#+id/list"
android:layout="#layout/setting_menu"
android:dialogTitle = "title one"
android:key = "key_1"
android:title = "ListPreference"
android:summary = "Summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListPreference>
<PreferenceScreen
android:layout="#layout/setting_menu"
android:key="Key two"
android:summary="Summary"
android:title="PreferenceScreen" />
<PreferenceScreen
android:layout="#layout/setting_title"
android:title = "Another Setting" />
The PreferenceScreen is a container, you are opening and closing him with noting inside.
You can create preferences like that:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:capitalize="words"
android:defaultValue="#string/default_value" <!--The initial state of the preference -->
android:key="#string/pref_key" <!-- The key for retrieve the preference -->
android:singleLine="true"
android:title="#string/pref_title" /> <!-- The title who is showing above the preference -->
</PreferenceScreen>
Edit: Create a xml file to with the layout of your preferences and create and Activity, example:
public class SettingsActivity extends PreferenceActivity
implements Preference.OnPreferenceChangeListener {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_general);
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_key))); // Is deprecated, but still a good option to no need change the values manually.
}
private void bindPreferenceSummaryToValue(Preference preference) {
preference.setOnPreferenceChangeListener(this);
onPreferenceChange(preference, PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
}
#Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
preference.setSummary(stringValue);
return true;
}
}

EditTextPreference, how to save value as float

How can i save value as float from EditTextPreference?
I follow this PreferenceActivity: save value as integer
But i did not work for me.
my preference file is
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Taxes"
android:key="first_category">
<CheckBoxPreference
android:key="include_taxes"
android:summary="Include or Exclude taxes"
android:title="Include Taxes"
android:defaultValue="true"
/>
<PreferenceScreen
android:key="Entering_Taxes"
android:title="Enter the Values"
android:dependency="include_taxes"
android:persistent="false">
<EditTextPreference
android:key="ED_tax"
android:title="E.D Tax"
android:summary="Define E.D Tax"
android:dialogTitle="Set E.D Tax"
android:dialogMessage="Enter the value"
android:defaultValue='7.30'/>
</PreferenceScreen>
</PreferenceCategory>
I have to add all values of taxes.
say you can get string of editTextPreference like this,
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String value = preferences.getString("SomeKey", "");
so convert that string to float like this:
float f = Float.valueOf(value);

Android + Preferences

I have a preferences.xml and Settings.java... In the onCreate method I want to set the summary of the preferences... Could someone guide me what is the best way to do it???
What I know is that one way of doing is
activityName_text = (EditTextPreference) findPreference("activityName");
activityName_text.setSummary(activityName_text.getText());
but for each key if I follow the above pattern I feel that the code will be bad...
Preferences.xml:
<PreferenceScreen android:title="Header" >
<EditTextPreference
android:defaultValue="TestRequest"
android:key="activityName"
android:title="activityName" />
<EditTextPreference
android:defaultValue="Request"
android:key="msgName"
android:title="msgName" />
<ListPreference
android:defaultValue="0"
android:entries="#array/msg_type"
android:entryValues="#array/msg_type_values"
android:key="msgType"
android:title="msgType" />
<EditTextPreference
android:defaultValue="AA"
android:key="senderURI"
android:title="senderURI" />
<EditTextPreference
android:defaultValue="BB"
android:key="destinationURI"
android:title="destinationURI" />
<EditTextPreference
android:defaultValue="AA"
android:key="replyToURI"
android:title="replyToURI" />
<EditTextPreference
android:defaultValue="BB"
android:key="originatorURI"
android:title="originatorURI" />
<EditTextPreference
android:defaultValue="FF"
android:key="failureReplytoURI"
android:title="failureReplytoURI" />
<EditTextPreference
android:defaultValue="AA"
android:key="correlationId"
android:title="correlationId" />
<EditTextPreference
android:defaultValue="HH"
android:key="security"
android:title="security" />
<EditTextPreference
android:defaultValue="GG"
android:key="securityType"
android:title="securityType" />
<EditTextPreference
android:defaultValue="HH"
android:key="priority"
android:title="priority" />
<ListPreference
android:defaultValue="0"
android:entries="#array/communicationPattern"
android:entryValues="#array/communicationPatternValues"
android:key="communicationPattern"
android:title="communicationPattern" />
<ListPreference
android:defaultValue="0"
android:entries="#array/communicationStyle"
android:entryValues="#array/communicationStyleValues"
android:key="communicationStyle"
android:title="communicationStyle" />
<EditTextPreference
android:defaultValue="100"
android:digits="0123456789"
android:key="requestedBatchSize"
android:title="requestedBatchSize" />
<!-- radhika-number format -->
<EditTextPreference
android:defaultValue="78"
android:digits="0123456789"
android:key="batchSequenceNumber"
android:title="batchSequenceNumber" />
<ListPreference
android:defaultValue="0"
android:entries="#array/batchSequenceEndOfReply"
android:entryValues="#array/batchSequenceEndOfReplyValues"
android:key="batchSequenceEndOfReply"
android:title="batchSequenceEndOfReply" />
<EditTextPreference
android:defaultValue="HH"
android:key="iteratorReferenceURI"
android:title="iteratorReferenceURI" />
<EditTextPreference
android:defaultValue="KK"
android:key="fileLocationURI"
android:title="fileLocationURI" />
<EditTextPreference
android:defaultValue="10/10/2010"
android:key="timestamp"
android:title="timestamp" />
<EditTextPreference
android:defaultValue="kkk"
android:key="vendorExtensions"
android:title="vendorExtensions" />
<PreferenceCategory
android:key="activityStatus"
android:title="activityStatus" >
<ListPreference
android:defaultValue="0"
android:entries="#array/activityStatusType"
android:entryValues="#array/activityStatusTypeValues"
android:key="actStatus"
android:title="activityStatus" />
<EditTextPreference
android:defaultValue="AA"
android:key="qualifier"
android:title="qualifier" />
</PreferenceCategory>
<PreferenceCategory
android:key="msgSpecificProperties"
android:title="msgSpecificProperties" >
<EditTextPreference
android:defaultValue="multiple values"
android:key="propName"
android:title="propName" />
<EditTextPreference
android:defaultValue="multiple values"
android:key="propValue"
android:title="propValue" />
</PreferenceCategory>
<PreferenceCategory
android:key="compressionType"
android:title="compressionType" >
<EditTextPreference
android:defaultValue="kkk"
android:key="compressionTypeAttr"
android:title="extension" />
<ListPreference
android:defaultValue="0"
android:entries="#array/compressionType"
android:entryValues="#array/compressionTypeValues"
android:key="compressionTypeValue"
android:title="value" />
</PreferenceCategory>
<PreferenceCategory
android:key="packingType"
android:title="packingType" >
<EditTextPreference
android:defaultValue="kkk"
android:key="packingTypeAttr"
android:title="extension" />
<ListPreference
android:defaultValue="0"
android:entries="#array/packingType"
android:entryValues="#array/packingTypeValues"
android:key="packingTypeValue"
android:title="value" />
</PreferenceCategory>
</PreferenceScreen>
<PreferenceScreen android:title="Body" >
<PreferenceScreen android:title="Managed Elements" >
<PreferenceCategory
android:key="mdOrMlsnRef"
android:title="md Or Mlsn Ref" >
<EditTextPreference
android:defaultValue="Name"
android:key="rdnName"
android:title="rdnName" />
<EditTextPreference
android:defaultValue="Value"
android:key="rdnValue"
android:title="rdnValue" />
</PreferenceCategory>
</PreferenceScreen>
<PreferenceScreen android:title="Active Alarms" >
</PreferenceScreen>
</PreferenceScreen>
</PreferenceScreen>
Settings.java:
package com.example.settingstest;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
public class Settings extends PreferenceActivity implements OnSharedPreferenceChangeListener {
EditTextPreference activityName_text;
EditTextPreference msgName_text;
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
#Override
protected void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
}
#Override protected void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Preference p = findPreference(key);
if(p instanceof EditTextPreference)
{
p.setSummary(sharedPreferences.getString(key, (String)p.getSummary()));
}
else if(p instanceof ListPreference)
{
p.setSummary((String)((ListPreference) p).getEntry());
}
}
}
Use like this its always better to set summary from prefrence.xml, I am giving one example I am using this code and its working perfectly
<PreferenceCategory
android:key="dbh_social"
android:title="We know you are social" >
<Preference
android:key="dbh_invite"
android:summary="Invite your friends"
android:title="Invite Facebook Friends" />
<Preference
android:key="dbh_wall_post"
android:summary="Post about us on your Facebook wall "
android:title="Post on Facebook Wall" />
</PreferenceCategory>
You can put all your keys into a string-array resource and iterate over it on onCreate()
final String[] keys = getResources().getStringArray(R.array.keys) ; // all your keys here
SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
for (int i = 0; i < keys.length; i++) {
setPreferenceSummary(p, keys[i]);
}
private void setPreferenceSummary(SharedPreferences sharedPreferences, String key) {
final Preference p = findPreference(key);
if(p instanceof EditTextPreference)
{
p.setSummary(sharedPreferences.getString(key, (String)p.getSummary()));
}
else if(p instanceof ListPreference)
{
p.setSummary((String)((ListPreference) p).getEntry());
}
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
setPreferenceSummary(sharedPreferences, key);
}

getString from SharedPreferences always return default value for one value

i'm using this code in an activity
protected void onResume() {
super.onResume();
prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
System.out.println(connections);
String dir = prefs.getString("download_directory", "Download");
System.out.println(dir);
}
My preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="Downloads">
<EditTextPreference
android:key="download_directory"
android:title="#string/download_destination"
android:summary="#string/download_description"
android:defaultValue="FacebookPhotos"
android:dialogTitle="#string/download_destination" />
<CheckBoxPreference android:key="subdir_album"
android:title="#string/download_album"
android:summary="#string/download_album_description" />
</PreferenceCategory>
<PreferenceCategory android:title="Gallery">
<EditTextPreference
android:key="connections"
android:title="Concurrent connections"
android:numeric="integer"
android:maxLength="2"
android:defaultValue="7"
android:dialogTitle="Concurrent connections" />
</PreferenceCategory>
</PreferenceScreen>
edited by
public class Preferences extends PreferenceActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
The problem is, when i get "connections" value it gives me the correct int, instead the getString for key "download_directory" always gives me the default value "Download"
getString for key "download_directory" always gives me the default
value "Download"
This can only happen if "download_directory" is not set at all!

Categories

Resources