Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialScott Mobley-Schreibman
7,791 PointsSharing age calculator app (Interactive Story app extra credit)
For extra credit after the Interactive Story app we were asked (among other things) to try building an app that would take a person's age in years and show in another activity that person's age in days, weeks, months, etc.
The app was supposed to use intents but I had better luck with radio buttons and toasts. I also decided to take the user's birth date for input instead of age in years since I was working with units smaller than years. But I wound up working with decimals and had to use doubles instead of integers, so the output looks like an age calculator parody. . .
If anyone can suggest ways to streamline this code and/or get rid of the decimal output, fire away!
<LinearLayout 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"
android:orientation="vertical"
tools:context=".AgeInputActivity"
android:background="@android:color/white"
android:weightSum="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ageInput"
android:src="@drawable/age_input_image"
android:adjustViewBounds="true"
android:scaleType="fitStart"
android:layout_weight="0.33"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Enter your birth date"
android:id="@+id/enterDob"
android:textColor="#ff1546ff"
android:textStyle="bold|italic"
android:paddingStart="@dimen/activity_horizontal_margin"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="date"
android:ems="10"
android:id="@+id/enterDay"
android:layout_weight="0.13"
android:hint="Day (dd)"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="date"
android:ems="10"
android:id="@+id/enterMonth"
android:layout_weight="0.13"
android:hint="Month (mm)"/>
<EditText
android:layout_width="350dp"
android:layout_height="wrap_content"
android:inputType="date"
android:ems="10"
android:id="@+id/enterYear"
android:layout_weight="0.13"
android:hint="Year (yyyy)"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Now calculate your age in. . ."
android:id="@+id/calculateAge"
android:layout_below="@+id/ageChoice"
android:textColor="#ff1546ff"
android:textStyle="bold|italic"
android:paddingStart="@dimen/activity_horizontal_margin"/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ageChoice"
android:orientation="vertical">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Days"
android:id="@+id/radioButtonDay"
android:onClick="OnRadioButtonClicked"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Weeks"
android:id="@+id/radioButtonWeek"
android:onClick="OnRadioButtonClicked"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Months"
android:id="@+id/radioButtonMonth"
android:onClick="OnRadioButtonClicked"/>
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:id="@+id/reset"
android:textColor="#ff1546ff"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
public class AgeInputActivity extends Activity {
private EditText textDay;
private EditText textMonth;
private EditText textYear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_age_input);
textDay = (EditText) findViewById(R.id.enterDay);
textMonth = (EditText) findViewById(R.id.enterMonth);
textYear = (EditText) findViewById(R.id.enterYear);
final RadioGroup ageChoice = (RadioGroup)findViewById(R.id.ageChoice);
Button reset = (Button)findViewById(R.id.reset);
reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textDay.setText("");
textMonth.setText("");
textYear.setText("");
ageChoice.clearCheck();
}
});
}
public void OnRadioButtonClicked(View view) {
int year = Integer.parseInt(textYear.getText().toString());
int month = Integer.parseInt(textMonth.getText().toString());
int day = Integer.parseInt(textDay.getText().toString());
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId()) {
case R.id.radioButtonDay:
if (checked)
Toast.makeText(this, "You are "+ (String.valueOf(convertToDays(getAge(year, month, day)))) + " days old", Toast.LENGTH_LONG).show();
break;
case R.id.radioButtonWeek:
if (checked)
Toast.makeText(this, "You are "+ (String.valueOf(convertToWeeks(getAge(year, month, day))))+ " weeks old", Toast.LENGTH_LONG).show();
break;
case R.id.radioButtonMonth:
if (checked)
Toast.makeText(this, "You are "+ (String.valueOf(convertToMonths(getAge(year, month, day))))+ " months old", Toast.LENGTH_LONG).show();
break;
}
}
public double getAge(int DOByear, int DOBmonth, int DOBday) {
double age;
final Calendar calendarToday = Calendar.getInstance();
int currentYear = calendarToday.get(Calendar.YEAR);
int currentMonth = 1 + calendarToday.get(Calendar.MONTH);
int todayDay = calendarToday.get(Calendar.DAY_OF_MONTH);
age = (((currentYear - DOByear)*365)+ ((currentYear - DOByear)/4))+ ((((currentMonth-1)*(365.25/12))+todayDay)- (((DOBmonth-1)*(365.25/12))+ DOBday));
if(DOBmonth > currentMonth){
--age;
}
else if (DOBmonth == currentMonth) {
if(DOBday > todayDay) {
--age;
}
}
return age;
}
private double convertToDays (double getAge) {
return (getAge);
}
private double convertToWeeks(double getAge) {
return ((getAge/365)*52);
}
private double convertToMonths(double getAge) {
return ((getAge/365) *12);
}
}
2 Answers
Harry James
14,780 PointsI have to say this is great code! It's a pleasure to read!
My only thing I would recommend you do is chain your if statements into single statements:
if(DOBmonth > currentMonth){
--age;
}
else if (DOBmonth == currentMonth) {
if(DOBday > todayDay) {
--age;
}
}
For your months, you could just do something like this:
if(DOBmonth > currentMonth || DOBmonth == currentMonth){
--age;
if(DOBday > todayDay && DOBmonth == currentMonth) {
--age;
}
}
You may even be able to make it better from there. I'm not so good at the Maths side of it so, I'll leave that to you :D
Scott Mobley-Schreibman
7,791 PointsThanks Harry! I implemented your second suggestion: more robust and easier to follow than what I had.
Harry James
14,780 PointsGlad to hear it helped!
Also, I did only write one suggestion but see how you could be confused that I put two (I should probably have clarified that...).
So, quick message to say sorry for the confusion and keep up the good work :)