Age Calculator
Make your age calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:background="#FFFFFF" | |
android:orientation="vertical"> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:gravity="center" | |
android:padding="7dp" | |
android:text="@string/app_name" | |
android:textColor="#FFFFFF" | |
android:textSize="22sp"/> | |
<ScrollView | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1" | |
android:background="#FFFFFF"> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="vertical"> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_margin="5dp" | |
android:orientation="vertical"> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1" | |
android:padding="10dp" | |
android:text="@string/from" | |
android:textColor="#000000" | |
android:textSize="17sp"/> | |
<View | |
android:layout_width="match_parent" | |
android:layout_height="2dp" | |
android:background="#777777"/> | |
<TextView | |
android:id="@+id/tv_fromDate" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1" | |
android:padding="10dp" | |
android:text="@string/select_from" | |
android:textColor="#666666" | |
android:textSize="17sp"/> | |
</LinearLayout> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_margin="5dp" | |
android:orientation="vertical"> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1" | |
android:padding="10dp" | |
android:text="@string/to" | |
android:textColor="#000000" | |
android:textSize="17sp"/> | |
<View | |
android:layout_width="match_parent" | |
android:layout_height="2dp" | |
android:background="#777777"/> | |
<TextView | |
android:id="@+id/tv_toDate" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1" | |
android:padding="10dp" | |
android:text="@string/select_to" | |
android:textColor="#666666" | |
android:textSize="17sp"/> | |
</LinearLayout> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_margin="5dp" | |
android:orientation="vertical"> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1" | |
android:padding="10dp" | |
android:text="@string/age" | |
android:textColor="#000000" | |
android:textSize="17sp"/> | |
<View | |
android:layout_width="match_parent" | |
android:layout_height="2dp" | |
android:background="#777777"/> | |
<TextView | |
android:id="@+id/tv_age" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1" | |
android:padding="10dp" | |
android:text="@string/your_age" | |
android:textColor="#666666" | |
android:textSize="17sp"/> | |
</LinearLayout> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_margin="5dp" | |
android:orientation="vertical"> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1" | |
android:padding="10dp" | |
android:text="@string/next_birthday" | |
android:textColor="#000000" | |
android:textSize="17sp"/> | |
<View | |
android:layout_width="match_parent" | |
android:layout_height="2dp" | |
android:background="#777777"/> | |
<TextView | |
android:id="@+id/tv_next_bday" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1" | |
android:padding="10dp" | |
android:text="@string/your_next_bday" | |
android:textColor="#666666" | |
android:textSize="17sp"/> | |
</LinearLayout> | |
</LinearLayout> | |
</ScrollView> | |
<Button | |
android:id="@+id/btn_calc" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center" | |
android:layout_margin="10dp" | |
android:padding="10dp" | |
android:text="@string/calculate" | |
android:textColor="#FFFFFF" | |
android:textSize="17sp"/> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.app.DatePickerDialog; | |
import android.app.Dialog; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.DatePicker; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.Date; | |
@SuppressWarnings("deprecation") | |
public class Calc extends Activity implements android.view.View.OnClickListener { | |
private TextView tv_fromDate = null, tv_toDate = null, tv_age = null, tv_next_bday = null; | |
private Button btn_calc = null; | |
private int[] monthDay = {31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; | |
private int year, month, day; | |
private int age = 0, myMonth, myDay, next_month, next_day; | |
private String formatedDate; | |
private Date fromDate, toDate; | |
private SimpleDateFormat sdf; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.claculator); | |
tv_fromDate = (TextView) findViewById(R.id.tv_fromDate); | |
tv_toDate = (TextView) findViewById(R.id.tv_toDate); | |
tv_age = (TextView) findViewById(R.id.tv_age); | |
tv_next_bday = (TextView) findViewById(R.id.tv_next_bday); | |
btn_calc = (Button) findViewById(R.id.btn_calc); | |
tv_fromDate.setOnClickListener(this); | |
tv_toDate.setOnClickListener(this); | |
btn_calc.setOnClickListener(this); | |
final Calendar c = Calendar.getInstance(); | |
year = c.get(Calendar.YEAR); | |
month = c.get(Calendar.MONTH); | |
day = c.get(Calendar.DATE); | |
c.set(year, month, day); | |
Date date = c.getTime(); | |
sdf = new SimpleDateFormat("dd-MMM-yyyy"); | |
formatedDate = sdf.format(date); | |
toDate = date; | |
tv_toDate.setText(formatedDate); | |
} | |
@Override | |
public void onClick(View v) { | |
switch (v.getId()) { | |
case R.id.tv_fromDate: | |
showDialog(R.id.tv_fromDate); | |
break; | |
case R.id.tv_toDate: | |
showDialog(R.id.tv_toDate); | |
break; | |
case R.id.btn_calc: | |
if (fromDate == null) { | |
Toast.makeText(Calc.this, "Please Select From Date", Toast.LENGTH_SHORT).show(); | |
showDialog(R.id.tv_fromDate); | |
} else if (toDate == null) { | |
Toast.makeText(Calc.this, "Please Select To Date", Toast.LENGTH_SHORT).show(); | |
showDialog(R.id.tv_toDate); | |
} else { | |
getAge(fromDate, toDate); | |
} | |
break; | |
} | |
} | |
@Override | |
protected Dialog onCreateDialog(int id) { | |
switch (id) { | |
case R.id.tv_fromDate: | |
return new DatePickerDialog(this, FromDatePicker, year, month, day); | |
case R.id.tv_toDate: | |
return new DatePickerDialog(this, ToDatePicker, year, month, day); | |
} | |
return null; | |
} | |
private DatePickerDialog.OnDateSetListener FromDatePicker = new DatePickerDialog.OnDateSetListener() { | |
// when dialog box is closed, below method will be called. | |
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) { | |
year = selectedYear; | |
month = selectedMonth; | |
day = selectedDay; | |
fromDate = new Date(year - 1900, month, day); | |
formatedDate = sdf.format(fromDate); | |
tv_fromDate.setText(formatedDate); | |
} | |
}; | |
private DatePickerDialog.OnDateSetListener ToDatePicker = new DatePickerDialog.OnDateSetListener() { | |
// when dialog box is closed, below method will be called. | |
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) { | |
year = selectedYear; | |
month = selectedMonth; | |
day = selectedDay; | |
toDate = new Date(year - 1900, month, day); | |
formatedDate = sdf.format(toDate); | |
tv_toDate.setText(formatedDate); | |
} | |
}; | |
public void getAge(Date dtFromDate, Date dtToDate) { | |
Calendar cToDate = Calendar.getInstance(); | |
Calendar cFromDate = Calendar.getInstance(); | |
cFromDate.setTime(dtFromDate); | |
cToDate.setTime(dtToDate); | |
if (cFromDate.after(cToDate) || cFromDate.equals(cToDate)) { | |
Toast.makeText(Calc.this, "To date must greter than From Date", Toast.LENGTH_LONG).show(); | |
} else { | |
age = cToDate.get(Calendar.YEAR) - cFromDate.get(Calendar.YEAR); | |
myMonth = cToDate.get(Calendar.MONTH) - cFromDate.get(Calendar.MONTH); | |
myDay = cToDate.get(Calendar.DATE) - cFromDate.get(Calendar.DATE); | |
int increment = 0; | |
if (cToDate.get(Calendar.DATE) < cFromDate.get(Calendar.DATE)) { | |
increment = this.monthDay[cFromDate.get(Calendar.MONTH) - 1]; | |
} | |
if (increment == -1) { | |
if ((cFromDate.get(Calendar.YEAR) / 4) == 0) { | |
increment = 29; | |
} else { | |
increment = 28; | |
} | |
} | |
if (increment != 0) { | |
myDay = (cToDate.get(Calendar.DATE) + increment) - cFromDate.get(Calendar.DATE); | |
next_day = increment - myDay; | |
Log.d("increment != 0 ", myDay + " " + increment + " " + next_day); | |
increment = 1; | |
} else { | |
myDay = cToDate.get(Calendar.DATE) - cFromDate.get(Calendar.DATE); | |
next_day = 31 - myDay; | |
Log.d("increment ", myDay + " " + increment + " " + next_day); | |
} | |
if (cToDate.get(Calendar.MONTH) < (cFromDate.get(Calendar.MONTH) + increment)) { | |
myMonth = ((12 + cToDate.get(Calendar.MONTH)) - (cFromDate.get(Calendar.MONTH) + increment)); | |
next_month = 11 - myMonth; | |
increment = 1; | |
} else { | |
myMonth = (cToDate.get(Calendar.MONTH) - (cFromDate.get(Calendar.MONTH) + increment)); | |
next_month = 11 - myMonth; | |
increment = 0; | |
} | |
// If birth date is greater than todays date (after 2 days adjustment of leap year) then decrement age one year | |
if ((cFromDate.get(Calendar.DAY_OF_YEAR) - cToDate.get(Calendar.DAY_OF_YEAR) > 3) || (cFromDate.get(Calendar.MONTH) > cToDate.get(Calendar.MONTH))) { | |
age--; | |
// If birth date and todays date are of same month and birth day of month is greater than todays day of month then decrement age | |
} else if ((cFromDate.get(Calendar.MONTH) == cToDate.get(Calendar.MONTH)) && (cFromDate.get(Calendar.DAY_OF_MONTH) > cToDate.get(Calendar.DAY_OF_MONTH))) { | |
age--; | |
} | |
setText(); | |
} | |
} | |
private void setText() { | |
if (age == 1 && myMonth == 1 && myDay == 1) { | |
tv_age.setText(age-- + "-Year " + myMonth-- + "-Month " + myDay + "-Day"); | |
} else if (age == 1 && myMonth == 1 && myDay != 1) { | |
tv_age.setText(age-- + "-Year " + myMonth-- + "-Month " + myDay + "-Days"); | |
} else if (age == 1 && myMonth != 1 && myDay == 1) { | |
tv_age.setText(age-- + "-Year " + myMonth-- + "-Months " + myDay + "-Day"); | |
} else if (age != 1 && myMonth == 1 && myDay == 1) { | |
tv_age.setText(age-- + "-Years " + myMonth-- + "-Month " + myDay + "-Day"); | |
} else if (age != 1 && myMonth != 1 && myDay == 1) { | |
tv_age.setText(age-- + "-Years " + myMonth-- + "-Months " + myDay + "-Day"); | |
} else if (age != 1 && myMonth == 1 && myDay != 1) { | |
tv_age.setText(age-- + "-Years " + myMonth-- + "-Month " + myDay + "-Days"); | |
} else if (age == 1 && myMonth != 1 && myDay != 1) { | |
tv_age.setText(age-- + "-Year " + myMonth-- + "-Months " + myDay + "-Days"); | |
} else { | |
tv_age.setText(age-- + "-Years " + myMonth-- + "-Months " + myDay + "-Days"); | |
} | |
if (next_month == 1 && next_day == 1) { | |
tv_next_bday.setText(next_month + "-Month " + next_day + "-Day"); | |
} else if (next_month != 1 && next_day == 1) { | |
tv_next_bday.setText(next_month + "-Months " + next_day + "-Day"); | |
} else if (next_month == 1 && next_day != 1) { | |
tv_next_bday.setText(next_month + "-Month " + next_day + "-Days"); | |
} else { | |
tv_next_bday.setText(next_month + "-Months " + next_day + "-Days"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<string name="app_name">Age Calculator</string> | |
<string name="action_settings">Settings</string> | |
<string name="from">From/Birth Date</string> | |
<string name="select_from">Tap to select- From/Birth Date</string> | |
<string name="to">To/Current Date</string> | |
<string name="select_to">Tap to select- To/Current Date</string> | |
<string name="age">Your Age</string> | |
<string name="your_age">Your Age is</string> | |
<string name="next_birthday">Next Birthday</string> | |
<string name="your_next_bday">Your Next Birthday In</string> | |
<string name="calculate">Calculate</string> | |
</resources> |
Comments
Post a Comment