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:andro xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"></p> <p><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" /></p> <p><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" /></p> <p><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" /></p> <p><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" /></p> <p></relativeLayout></p> <p>

Screen shot


Source code and Apk file download from the following link
HERE

11 thoughts on “Android Age Calculator”

Leave a Comment