0

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

1 Answers1

0

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

Waza_Be
  • 39,407
  • 49
  • 186
  • 260
  • it gave me error and i couldn't add that JAR, is it exist or i have to create it or what exactly – user2330447 May 08 '13 at 23:59
  • the whole app won't start at all it gives me an error just as i run it. actually i'm still kind of beginner at all this specially to android – user2330447 May 09 '13 at 00:10
  • after applying ur answer it worked but i still can't extract the age from the selected date any help plz? – user2330447 May 09 '13 at 05:16
  • Look at this very similar one (replace "-" by ".") http://stackoverflow.com/a/11790060/327402 – Waza_Be May 09 '13 at 05:20