Question

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

Was it helpful?

Solution

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.

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top