how to calculate age from birth date in shared preference - android

Hello everyone i'm developing android app that takes the birth date of a baby in a preference screen so the app can save it. but what i need after retrieving the value is to calculate the age to use it in different activities in the same app so what can i do and is it possible to convert the string(birth date) into int age??
here is the code for pref.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference
android:title="Enter baby name"
android:key="name"
android:summary="Enter baby name"
android:defaultValue="0"
/>
<EditTextPreference
android:persistent="true"
android:key="age"
android:title="Enter Baby birthday "
android:summary="baby age"
/>
<ListPreference
android:title="Gender"
android:key="list"
android:entries="#array/list"
/>
</PreferenceScreen>
and for getting them
SharedPreferences getPref =
PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String text = getPref.getString("name", " ");
String age = getPref.getString("age", " ");
please help

This kind of library might help: https://github.com/bostonandroid/DatePreference
<org.bostonandroid.datepreference.DatePreference
android:key="age" android:title="#string/babybirthday"
android:defaultValue="1991.01.01" />
Then I guess you are skilled enough to substract 2 Dates ;-)
If not, the link given by Lucifer in comment is very good.

Related

how to display preference values

I'm trying to migrate the preferences part of my app because of warning
default constructor in android.preference.preferenceactivity is deprecated
But, even after reading a lot of ressources, I don't understand why I can't have 2 preference values (string and integer) displayed in front of (or below) the EditTextPreference as I had before.
"Compte" (in the capture) (String) is enabled="false" because I don't want the user can edit it. But I want the user can see the account name in preferences.
"Nom de jours" (Integer) can be edited, if the user click on it, but I want the value is shown without opening editing mode.
prefs.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory app:title="#string/prefsCatGlobalTitle">
<EditTextPreference
app:enabled="false"
app:key="param_email"
app:title="#string/cnxEmailLabel" />
</PreferenceCategory>
<PreferenceCategory app:title="#string/prefsCatAlertsTitle">
<EditTextPreference
app:defaultValue="7"
android:inputType="number"
app:key="prefsAlertNbDays"
android:maxLength="2"
app:maxLines="1"
android:selectAllOnFocus="true"
app:singleLine="true"
app:title="#string/prefsAlertNbOfDays" />
<SwitchPreference
app:defaultValue="true"
app:key="prefsAlertNotif"
app:title="#string/prefsAlertNotifTitle" />
<SwitchPreference
app:defaultValue="false"
app:key="prefsAlertEmail"
app:title="#string/prefsAlertEmailTitle" />
<SwitchPreference
app:defaultValue="false"
app:key="prefsAlertCal"
app:title="#string/prefsAlertCalTitle" />
</PreferenceCategory>
<PreferenceCategory app:title="#string/prefsCatOthersTitle">
<SwitchPreference
app:defaultValue="false"
app:key="prefsAlertAutoSync"
app:title="#string/prefsAlertAutoSync" />
</PreferenceCategory>
</PreferenceScreen>
Preference capture
Apart from the visualization problem, everything works fine and my preferences are well saved and usable in my java code.
I can't find what I'm doing wrong or what I'm missing.
Thanks to #cmak
I never found this app:useSimpleSummaryProvider option before.

Android Preferences defaultValue not works

Here is my preferences.xml
<PreferenceCategory android:title="#string/pref_cat_recognition">
<ListPreference
android:entries="#array/unitsArray"
android:entryValues="#array/unitsValues"
android:icon="#null"
android:key="unit"
android:summary="#string/units_summary"
android:title="#string/units"
android:defaultValue="1"/>
</PreferenceCategory>
and my array.xml
<string-array name="unitsArray" translatable="false">
<item>cm</item>
<item>inch</item>
</string-array>
<string-array name="unitsValues" translatable="false">
<item>1</item>
<item>2</item>
</string-array>
So it should be cm checked on start but there is no value checked. Why and how to solve that to have a value cm on start?
Your code is working fine. The reason why you dont see the selected value on the summary is because you have specified a custom summary text. You can use useSimpleSummaryProvider to enable the simple version of summary, also enabling the selected value seen without opening the preference.
Pls check my code below for clarity of things:
`<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory app:title="Choose Gender">
<ListPreference
app:defaultValue="m"
app:entries="#array/gender_entries"
app:entryValues="#array/gender_values"
app:key="gender"
app:title="Your Gender"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>
<PreferenceCategory app:title="StackOverflow">
<ListPreference
app:defaultValue="1"
app:entries="#array/unitsArray"
app:entryValues="#array/unitsValues"
app:key="unit"
app:title="Units"
app:useSimpleSummaryProvider="true" />
<ListPreference
app:defaultValue="1"
app:entries="#array/unitsArray"
app:entryValues="#array/unitsValues"
app:key="unit"
app:summary="Your summary goes here"
app:title="Units" />
</PreferenceCategory>
`
This is the preference screen view
If the summary was provided you'd be seeing the summary text and not the default/selected value like in the picture above.
It turned out in manifest I had android:allowBackup="true"

Validate edittext in PreferenceFragment

I have an EditTextPreference for the user to enter a Bluetooth MAC address in my preferences.xml file. I am using android:digits to limit input to valid characters, but that will not prevent someone from adding too little or too many characters, double colons, etc. If they do these things, the app crashes with an invalid MAC address exception. Is there a way to do this programmatically?
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Bluetooth Settings">
<EditTextPreference
android:key="prefMacAddress"
android:title="MAC Address"
android:lines="1"
android:maxLines="1"
android:summary="Enter MAC Address of the Module"
android:dialogTitle="MAC Address"
android:dialogMessage="Enter MAC Address of the Module"
android:defaultValue="20:13:07:26:24:32"
android:digits="0123456789abcdef:"/>
</PreferenceCategory>
</PreferenceScreen>
Use regular expressions for input in your EditText. Here's a good tutorial: http://www.vogella.com/tutorials/JavaRegularExpressions/article.html

How to load and store values of the preferences screen into SQLite DB?

just added a Preference-Screen into my app. I want to load and store the given values into my sqlite db. I do not know how to solve that, because I dont know how to access the fields in the oncreate of my method, they have no android:id field but an android:key
Here is my layout
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="Preferences">
<PreferenceCategory
android:title="#string/program_options">
<CheckBoxPreference
android:key="pref_opt1"
android:title="#string/autoLogin"
android:summary="#string/autoLoginDescription"
android:defaultValue="true"
/>
</PreferenceCategory>
<PreferenceCategory
android:title="#string/program_informations">
<ListPreference
android:key="pref_type"
android:title="Type"
android:summary="Select item from array"
android:entries="#array/types"
android:entryValues="#array/types_values"
android:defaultValue="1"
/>
<EditTextPreference
android:key="pref_text"
android:title="Input text"
android:summary="Tap to enter some text"
android:dialogTitle="Enter text"
/>
</PreferenceCategory>
<Preference
android:title="Visit us"
android:summary="SRS-Management GmbH">
<intent
android:action="android.intent.action.VIEW"
android:data="http://paperdynamix.de/" />
</Preference>
</PreferenceScreen>
and my Activity where i want to access the fields
package de.srs.android.pdixuploader.activies;
import de.srs.android.pdixuploader.R;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.widget.CheckBox;
public class Prefs extends PreferenceActivity {
private CheckBox autoLogin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//here I want to read and prefill the values of the fields
addPreferencesFromResource(R.layout.preferences);
}
}
Thanks
First of all there is no need to "prefill" the values since Android handels all the needed values by itself (first time the default values will be used).
In order to get the values just use the following code:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
// For example
Boolean pref_opt1 = sp.getBoolean("pref_opt1", true)
// generally
// sp.get[TYPE]("KEY", STANDARD-VALUE);

Android Default Preference xml file format

I appear to be the only person on the internet who can't figure this out, but when I use
PreferenceManager.setDefaultValues(this, R.xml.preference, false);
what is the syntax of the XML file for the default settings. I simply want to set two preferences to true.
I have tried the normal android string resource format, but nothing seems to work. Thanks ahead of time.
Use android:defaultValue like so:
<CheckBoxPreference
android:key="my_pref"
android:defaultValue="true"
android:title="#string/pref_title_my_pref"
android:summary="#string/pref_summary_my_pref"/>
You are not the only person who doesn't know this. Even am struggling to know the file format of the XML. Finally i found the answer! There you go!
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference android:key="CLIENT_HOMESCREEN_TITLE"
android:defaultValue="Home Screen"/>
<EditTextPreference android:key="CLIENT_ADMIN_BUTTON"
android:defaultValue="Admin"/>
<EditTextPreference android:key="CLIENT_PLAYER_BUTTON"
android:defaultValue="Player"/>
<EditTextPreference android:key="CLIENT_SAVE_BUTTON"
android:defaultValue="Save"/>
<EditTextPreference android:key="CLIENT_CANCEL_BUTTON"
android:defaultValue="Cancel"/>
<EditTextPreference android:key="CLIENT_SERVER_SETUP_IP"
android:defaultValue="IP:"/>
<EditTextPreference android:key="CLIENT_SERVER_SETUP_PORT"
android:defaultValue="Port:"/>
<EditTextPreference android:key="CLIENT_SERVER_SETUP_TITLE"
android:defaultValue="Server Setup"/>
</PreferenceScreen>
what is the syntax of the XML file for the default settings. I simply want to set two preferences to true.
For this just set default value to true
Whatever default values you have provided in preference xml file, that will be set.

Categories

Resources