How to set/update values of my preferences (PreferencesAcitivity)?Android - android

I have a SettingsActivity ( extends PreferenceActivity ) , which is loaded from a preferences.xml file(res-->xml-->preferences.xml).
The preferences.xml is this:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceCategory android:title="Patient&apos;s Settings" >
<EditTextPreference
android:defaultValue="Not Set"
android:key="patientMobile"
android:title="mobile number" />
</PreferenceCategory>
<PreferenceCategory android:title="Doctor&apos;s Settings" >
<EditTextPreference
android:defaultValue="Not Set"
android:key="docEmail"
android:title="e-mail" />
<EditTextPreference
android:defaultValue="Not Set"
android:key="docMobile"
android:title="mobile number" />
</PreferenceCategory>
<PreferenceCategory android:title="Application Settings" >
<SwitchPreference
android:disableDependentsState="false"
android:enabled="true"
android:key="lang"
android:summaryOff="English"
android:summaryOn="Greek"
android:switchTextOff="EN"
android:switchTextOn="GR" />
</PreferenceCategory>
How i can set/update/overwrite these values from another activity?
I retreive information from a webservice and then i want to save these values and then to be seen from SettingsActivity:
1.patientMobile (string)
2.docEmail (string)
3.docMobile (string)

You can read/set/update these values using SharedPreferences and the key to the preference.
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String patientMobile = preferences.getString("patientMobile");
//or set the values.
SharedPreferences.Editor editor = preferences.edit();
editor.putString("patientMobile", "yes"); //This is just an example, you could also put boolean, long, int or floats
editor.commit();

When you retrieve values you save them like:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(YourActivity.this);
Editor editor = sp.edit();
editor.putString("patientMobile", patientMobile);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) {
editor.apply();
} else {
editor.commit();
}
To read values simply call sp.getString("patientMobile", defaultValue);
See apply(). Also SharedPreferences docs.

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);

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!

Display Summary on Preference Screen 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!

Categories

Resources