Android Age Calculator

In this application we calculate total year, months and day of our age.
You simply enter Data of Birth to calculate it.
Let start the fun
First we Create AgeCalcualtion class where we add all calculation method to calculate Year, Months and Day from date of Birth
public class AgeCalculation {
private int startYear;
private int startMonth;
private int startDay;
private int endYear;
private int endMonth;
private int endDay;
private int resYear;
private int resMonth;
private int resDay;
public String getCurrentDate()
{
Calendar c=Calendar.getInstance();
endYear=c.get(Calendar.YEAR);
endMonth=c.get(Calendar.MONTH);
endMonth++;
endDay=c.get(Calendar.DAY_OF_MONTH);
return endDay+":"+endMonth+":"+endYear;
}
public void setDateOfBirth(int sYear, int sMonth, int sDay)
{
startYear=sYear;
startMonth=sMonth;
startMonth++;
startDay=sDay;

}
public void calcualteYear()
{
resYear=endYear-startYear;

}

public void calcualteMonth()
{
if(endMonth>=startMonth)
{
resMonth= endMonth-startMonth;
}
else
{
resMonth=endMonth-startMonth;
resMonth=12+resMonth;
resYear--;
}

}
public void calcualteDay()
{

if(endDay>=startDay)
{
resDay= endDay-startDay;
}
else
{
resDay=endDay-startDay;
resDay=30+resDay;
//resMonth--;
}
}

public String getResult()
{
return resDay+":"+resMonth+":"+resYear;
}

}


Now we Activity class where we add a button . When user click on the button Date Picker Dialog will be display. When user Set Data of Birth and enter Set button .All calculation will be done from AgeCalculation class.
Here is the activity class code.
public class MainActivity extends Activity implements OnClickListener{
private Button btnStart;
static final int DATE_START_DIALOG_ID = 0;
private int startYear=1970;
private int startMonth=6;
private int startDay=15;
private AgeCalculation age = null;
private TextView currentDate;
private TextView birthDate;
private TextView result;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
age=new AgeCalculation();
currentDate=(TextView) findViewById(R.id.textView1);
currentDate.setText( age.getCurrentDate());
birthDate=(TextView) findViewById(R.id.textView2);
result=(TextView) findViewById(R.id.textView3);
btnStart=(Button) findViewById(R.id.button1);
btnStart.setOnClickListener(this);

}

@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_START_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
startYear, startMonth, startDay);
}
return null;
}

private DatePickerDialog.OnDateSetListener mDateSetListener
= new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
startYear=selectedYear;
startMonth=selectedMonth;
startDay=selectedDay;
age.setDateOfBirth(startYear, startMonth, startDay);
birthDate.setText(""+selectedDay+":"+(startMonth+1)+":"+startYear);
calculateAge();
}
};
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
showDialog(DATE_START_DIALOG_ID);
break;

default:
break;
}
}
private void calculateAge()
{
age.calcualteYear();
age.calcualteMonth();
age.calcualteDay();
Toast.makeText(getBaseContext(), "click the resulted button"+age.getResult() , Toast.LENGTH_SHORT).show();
result.setText(age.getResult());
}
}



Now we write the layout of Activity class
<br /> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" ><br /> <br /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="Date Of Birth" /><br /> <br /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/button1" android:layout_marginTop="23dp" android:text="Current Date" /><br /> <br /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView1" android:layout_marginTop="18dp" android:text="Birth Date" /><br /> <br /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView2" android:layout_marginTop="16dp" android:text="Result" /><br /> <br /> </RelativeLayout><br /> <br />


Screen shot

Source code and Apk file download from the following link
HERE



Post a Comment

12 Comments

  1. Getting error in MainActivity.java

    AgeCalculation can not be resolved to a type.

    ReplyDelete
    Replies
    1. I totally tested it and its work warm good. Please download Source code and Apk for testing and easily implementation

      Delete
  2. Hello,

    I am facing an issue with datepicker dialogue.

    It is setting the date even when clicking outside of the dialogue.

    ReplyDelete
    Replies
    1. Please First of all simply run a small code of date picker which are easily available on google then try this apps. Thanks

      Delete
  3. If you want to know your actual age..........
    Download Age Calculator now from Softonic: 100% safe and virus free. More than 2399 downloads this month.

    Age Calculator APK

    ReplyDelete
  4. if you want to track your any of your friends cell phone. then download the cell phone tracker apk for free.

    ReplyDelete
  5. @Saadat Rahim: Great job bro !

    ReplyDelete
  6. Nice Tutorial but need some changes in AgeCalculation() Functions brother.

    import java.util.Calendar;

    /**
    * Created by Prash on 9/6/2017.
    */

    public class AgeCalculation {
    private int startYear;
    private int startMonth;
    private int startDay;
    private int endYear;
    private int endMonth;
    private int endDay;
    private int resYear;
    private int resMonth;
    private int resDay;

    public String getCurrentDate()
    {
    Calendar c= Calendar.getInstance();
    endYear=c.get(Calendar.YEAR);
    endMonth=c.get(Calendar.MONTH);
    endMonth++;
    endDay=c.get(Calendar.DAY_OF_MONTH);
    return endDay+":"+endMonth+":"+endYear;
    }
    public void setDateOfBirth(int sYear, int sMonth, int sDay)
    {
    startYear=sYear;
    startMonth=sMonth;
    //startMonth++;
    startDay=sDay;

    }
    public void calcualteYear()
    {
    resYear=endYear-startYear;
    }

    public void calcualteMonth()
    {
    if(endMonth>=startMonth)
    {
    if(startMonth == endMonth){

    if(endDay>=startDay){
    resMonth = 0;
    } else {
    resMonth= endMonth-startMonth;
    resMonth=12+resMonth-1;
    }
    resYear--;
    } else {
    resMonth= endMonth-startMonth;

    if(resMonth == 1){
    resMonth = 0;
    } else {
    resMonth = resMonth - 1;
    }
    }
    }
    else
    {
    resMonth=endMonth-startMonth;
    resMonth=12+resMonth-1;
    resYear--;
    }
    }
    public void calcualteDay()
    {

    if(endDay>=startDay)
    {
    resDay= endDay-startDay;
    }
    else
    {
    resDay=endDay-startDay;
    resDay=30+resDay;
    //resMonth--;
    }
    }

    public String getResult()
    {
    return resDay+":"+resMonth+":"+resYear;
    }

    public int getDay(){
    return resDay;
    }

    public int getMonth(){
    return resMonth;
    }

    public int getYear(){
    return resYear;
    }

    }

    ReplyDelete
  7. how to calculate age in months

    ReplyDelete
  8. can u please provide the code for calculating age in months

    ReplyDelete