<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/icons">
<ScrollView
android:id="@+id/scroll_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="@+id/display_date_result"
android:layout_marginBottom="6dp"
android:background="#ffffff"
android:scrollbars="none">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/current_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:layout_alignParentTop="true"
android:text="@string/date_today"
android:textStyle="bold"
android:textSize="16sp"
android:background="@color/primary_light"
android:textColor="@color/primary_text" />
<LinearLayout
android:id="@+id/first_layout_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/current_date"
android:padding="8dp"
android:background="@drawable/border_colour"
android:orientation="horizontal">
<TextView
android:id="@+id/display_today_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="12dp"
android:text="@string/display_date"
android:textStyle="bold"
android:textSize="16sp"
android:layout_weight="1"
android:textColor="@color/primary_text" />
<ImageView
android:id="@+id/set_date_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/app_name"
android:layout_weight="1"
android:layout_gravity="right|center_vertical"
android:src="@drawable/dateicon"/>
</LinearLayout>
<TextView
android:id="@+id/birthday_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:layout_below="@+id/first_layout_wrapper"
android:layout_marginTop="32dp"
android:text="@string/birthday_date"
android:textStyle="bold"
android:textSize="16sp"
android:background="@color/primary_light"
android:textColor="@color/primary_text" />
<LinearLayout
android:id="@+id/second_layout_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/birthday_date"
android:padding="8dp"
android:background="@drawable/border_colour"
android:orientation="horizontal">
<TextView
android:id="@+id/display_birthday_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="12dp"
android:text="@string/display_date"
android:textStyle="bold"
android:textSize="16sp"
android:layout_weight="1"
android:textColor="@color/primary_text" />
<ImageView
android:id="@+id/set_birthday_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/app_name"
android:layout_weight="1"
android:layout_gravity="right|center_vertical"
android:padding="0dp"
android:layout_margin="0dp"
android:src="@drawable/dateicon"/>
</LinearLayout>
<LinearLayout
android:id="@+id/button_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/second_layout_wrapper"
android:layout_marginTop="32dp"
android:orientation="horizontal">
<Button
android:id="@+id/button_calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center|center_horizontal"
android:padding="16dp"
android:text="@string/calculation_text"
android:layout_marginRight="1dp"
android:background="@color/primary_dark"
android:layout_weight="1"/>
<Button
android:id="@+id/button_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center|center_horizontal"
android:padding="16dp"
android:layout_marginLeft="1dp"
android:text="@string/clear_results"
android:background="@color/primary_dark"
android:layout_weight="1"/>
</LinearLayout>
<TextView
android:id="@+id/age_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:layout_below="@+id/button_wrapper"
android:layout_marginTop="32dp"
android:text="@string/current_age"
android:textStyle="bold"
android:textSize="16sp"
android:background="@color/primary_light"
android:textColor="@color/primary_text" />
<TableLayout
android:id="@+id/age_table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/age_header">
<TableRow
android:background="@drawable/border_colour"
android:padding="24dp">
<TextView
android:id="@+id/years_in_age"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center|center_horizontal"
android:text="@string/years_number"
android:textColor="@color/primary_text"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:id="@+id/months_in_age"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center|center_horizontal"
android:text="@string/months_number"
android:textColor="@color/primary_text"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:id="@+id/days_in_age"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center|center_horizontal"
android:text="@string/days_number"
android:textColor="@color/primary_text"
android:textSize="16dp"
android:textStyle="bold" />
</TableRow>
</TableLayout>
<TextView
android:id="@+id/next_birthday_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:layout_below="@+id/age_table"
android:layout_marginTop="32dp"
android:text="@string/next_birthday"
android:textStyle="bold"
android:textSize="16sp"
android:background="@color/primary_light"
android:textColor="@color/primary_text" />
<TableLayout
android:id="@+id/next_age_table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/next_birthday_header">
<TableRow
android:background="@drawable/border_colour"
android:padding="24dp">
<TextView
android:id="@+id/years_in_birthday"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center|center_horizontal"
android:text="@string/years_number"
android:textColor="@color/primary_text"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:id="@+id/months_in_birthday"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center|center_horizontal"
android:text="@string/months_number"
android:textColor="@color/primary_text"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:id="@+id/days_in_birthday"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center|center_horizontal"
android:text="@string/days_number"
android:textColor="@color/primary_text"
android:textSize="16dp"
android:textStyle="bold" />
</TableRow>
</TableLayout>
<TextView
android:id="@+id/age_analysis"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:layout_below="@+id/next_age_table"
android:layout_marginTop="32dp"
android:text="@string/age_analysis"
android:textStyle="bold"
android:textSize="16sp"
android:background="@color/primary_light"
android:textColor="@color/primary_text" />
<TableLayout
android:id="@+id/extra_information"
android:layout_width="match_parent"
android:layout_below="@+id/age_analysis"
android:layout_marginTop="24dp"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:layout_height="wrap_content">
<TableRow android:padding="8dp">
<TextView
android:id="@+id/total_years"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="16dp"
android:textStyle="bold"
android:textColor="@color/primary_text"
android:gravity="left"
android:text="@string/total_years"/>
<TextView
android:id="@+id/total_years_result"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="16dp"
android:textStyle="bold"
android:textColor="@color/primary_text"
android:gravity="left"
android:text="@string/zero"/>
</TableRow>
<TableRow android:padding="8dp">
<TextView
android:id="@+id/total_months"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="16dp"
android:textStyle="bold"
android:textColor="@color/primary_text"
android:gravity="left"
android:text="@string/total_months"/>
<TextView
android:id="@+id/total_months_result"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="16dp"
android:textStyle="bold"
android:textColor="@color/primary_text"
android:gravity="left"
android:text="@string/zero"/>
</TableRow>
<TableRow android:padding="8dp">
<TextView
android:id="@+id/total_weeks"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="@string/total_months"
android:textColor="@color/primary_text"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:id="@+id/total_weeks_result"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="@string/zero"
android:textColor="@color/primary_text"
android:textSize="16dp"
android:textStyle="bold" />
</TableRow>
<TableRow android:padding="8dp">
<TextView
android:id="@+id/total_days"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="@string/total_days"
android:textColor="@color/primary_text"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:id="@+id/total_days_result"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="@string/zero"
android:textColor="@color/primary_text"
android:textSize="16dp"
android:textStyle="bold" />
</TableRow>
<TableRow android:padding="8dp">
<TextView
android:id="@+id/total_hours"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="@string/total_hours"
android:textColor="@color/primary_text"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:id="@+id/total_hours_result"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="@string/zero"
android:textColor="@color/primary_text"
android:textSize="16dp"
android:textStyle="bold" />
</TableRow>
<TableRow android:padding="8dp">
<TextView
android:id="@+id/total_minutes"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="@string/total_minutes"
android:textColor="@color/primary_text"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:id="@+id/total_minutes_result"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="@string/zero"
android:textColor="@color/primary_text"
android:textSize="16dp"
android:textStyle="bold" />
</TableRow>
<TableRow android:padding="8dp">
<TextView
android:id="@+id/total_seconds"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="@string/total_seconds"
android:textColor="@color/primary_text"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:id="@+id/total_seconds_result"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="@string/zero"
android:textColor="@color/primary_text"
android:textSize="16dp"
android:textStyle="bold" />
</TableRow>
</TableLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
package com.inducesmile.completecalculatorapp; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.app.AppCompatActivity; import android.text.Html; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.DatePicker; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import org.joda.time.DateTime; import org.joda.time.LocalDate; import org.joda.time.LocalTime; import org.joda.time.Period; import org.joda.time.PeriodType; import org.joda.time.Weeks; import java.util.Calendar; public class AgeCalculatorFragment extends Fragment { private TextView todayDate; private ImageView todayDateSelector; private DatePicker todayDateDialog; private TextView yourBirthdayDate; private ImageView yourBirthdayDateSelector; private int year; private int month; private int day; public static final int DATE_PICKER_FRAGMENT = 1; public static final int SECOND_DATE_PICKER_FRAGMENT = 2; private TextView currentBirthdayYear; private TextView currentBirthdayMonth; private TextView currentBirthdayDay; private TextView nextBirthdayYear; private TextView nextBirthdayMonth; private TextView nextBirthdayDay; private TextView totalYearResult; private TextView totalMonthResult; private TextView totalWeekResult; private TextView totalDayResult; private TextView totalHourResult; private TextView totalMinuteResult; private TextView totalSecondResult; public AgeCalculatorFragment() { } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_age_calculator, container, false); ((AppCompatActivity) getActivity()).getSupportActionBar().show(); currentBirthdayYear = (TextView)view.findViewById(R.id.years_in_age); currentBirthdayMonth = (TextView)view.findViewById(R.id.months_in_age); currentBirthdayDay = (TextView)view.findViewById(R.id.days_in_age); nextBirthdayYear = (TextView)view.findViewById(R.id.years_in_birthday); nextBirthdayMonth = (TextView)view.findViewById(R.id.months_in_birthday); nextBirthdayDay = (TextView)view.findViewById(R.id.days_in_birthday); totalYearResult = (TextView)view.findViewById(R.id.total_years_result); totalMonthResult = (TextView)view.findViewById(R.id.total_months_result); totalWeekResult = (TextView)view.findViewById(R.id.total_weeks_result); totalDayResult = (TextView)view.findViewById(R.id.total_days_result); totalHourResult = (TextView)view.findViewById(R.id.total_hours_result); totalMinuteResult = (TextView)view.findViewById(R.id.total_minutes_result); totalSecondResult = (TextView)view.findViewById(R.id.total_seconds_result); todayDate = (TextView)view.findViewById(R.id.display_today_date); todayDateSelector = (ImageView)view.findViewById(R.id.set_date_icon); yourBirthdayDate = (TextView)view.findViewById(R.id.display_birthday_date); yourBirthdayDateSelector = (ImageView)view.findViewById(R.id.set_birthday_icon); Button calculateButton = (Button)view.findViewById(R.id.button_calculate); Button clearButton = (Button)view.findViewById(R.id.button_clear); todayDate.setText(setCurrentDateOnView()); yourBirthdayDate.setText(setCurrentDateOnView()); todayDateSelector.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { DatePickerFragment datePickerDialog = new DatePickerFragment(); datePickerDialog.setTargetFragment(AgeCalculatorFragment.this, DATE_PICKER_FRAGMENT); datePickerDialog.show(getFragmentManager(), "DATE TODAY"); } }); yourBirthdayDateSelector.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { DatePickerFragment datePickerDialog = new DatePickerFragment(); datePickerDialog.setTargetFragment(AgeCalculatorFragment.this, SECOND_DATE_PICKER_FRAGMENT); datePickerDialog.show(getFragmentManager(), "BIRTHDAY TODAY"); } }); calculateButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { String getTodayDate = todayDate.getText().toString(); String getBirthdayDate = yourBirthdayDate.getText().toString(); DateTime todayDateTime = convertToDateTime(getTodayDate); DateTime birthdayDateTime = convertToDateTime(getBirthdayDate); displayCurrentBirthday(todayDateTime, birthdayDateTime); displayNextBirthday(todayDateTime, birthdayDateTime); displayAgeAnalysis(todayDateTime, birthdayDateTime); } }); clearButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { currentBirthdayDay.setText(Html.fromHtml("<h4>Days</h4>")); currentBirthdayMonth.setText(Html.fromHtml("<h4>Months</h4>")); currentBirthdayYear.setText(Html.fromHtml("<h4>Years</h4>")); nextBirthdayDay.setText(Html.fromHtml("<h4>Days</h4>")); nextBirthdayMonth.setText(Html.fromHtml("<h4>Months</h4>")); nextBirthdayYear.setText(Html.fromHtml("<h4>Years</h4>")); totalDayResult.setText(""); totalWeekResult.setText(""); totalMonthResult.setText(""); totalYearResult.setText(""); totalHourResult.setText(""); totalMinuteResult.setText(""); totalSecondResult.setText(""); } }); return view; } private void displayAgeAnalysis(DateTime dateToday, DateTime birthdayDate){ Period dateDifferencePeriod = displayBirthdayResult(dateToday, birthdayDate); int getDateInDays = dateDifferencePeriod.getDays(); int getDateInWeeks = Weeks.weeksBetween(new DateTime(birthdayDate), new DateTime(dateToday)).getWeeks();; int getDateInMonths = dateDifferencePeriod.getMonths(); int getDateInYears = dateDifferencePeriod.getYears(); int mDay = getDateInWeeks * 7; int mMonth = getDateInMonths + (getDateInYears * 12); int hours = mDay * 24; int minutes = mDay * 24 * 60; int seconds = mDay * 24 * 60 * 60; totalDayResult.setText(Html.fromHtml(String.valueOf(mDay))); totalWeekResult.setText(Html.fromHtml(String.valueOf(getDateInWeeks))); totalMonthResult.setText(Html.fromHtml(String.valueOf(mMonth))); totalYearResult.setText(Html.fromHtml(String.valueOf(getDateInYears))); totalHourResult.setText(Html.fromHtml(String.valueOf(hours))); totalMinuteResult.setText(Html.fromHtml(String.valueOf(minutes))); totalSecondResult.setText(Html.fromHtml(String.valueOf(seconds))); } private void displayNextBirthday(DateTime dateToday, DateTime birthdayDate){ Calendar mCalendar = Calendar.getInstance(); int year = mCalendar.get(Calendar.YEAR); DateTime nextBirthday = birthdayDate.withYear(year); Toast.makeText(getActivity(), "Birthday " + nextBirthday.getYear(), Toast.LENGTH_LONG).show(); Period dateDifferencePeriod = displayBirthdayResult(nextBirthday, dateToday); int getDateInDays = dateDifferencePeriod.getDays(); int getDateInMonths = dateDifferencePeriod.getMonths(); int getDateInYears = dateDifferencePeriod.getYears(); nextBirthdayDay.setText(Html.fromHtml("<h4>Days</h4>" + getDateInDays)); nextBirthdayMonth.setText(Html.fromHtml("<h4>Months</h4>" + getDateInMonths)); nextBirthdayYear.setText(Html.fromHtml("<h4>Years</h4>" + getDateInYears)); } private void displayCurrentBirthday(DateTime dateToday, DateTime birthdayDate){ Period dateDifferencePeriod = displayBirthdayResult(dateToday, birthdayDate); int getDateInDays = dateDifferencePeriod.getDays(); int getDateInMonths = dateDifferencePeriod.getMonths(); int getDateInYears = dateDifferencePeriod.getYears(); currentBirthdayDay.setText(Html.fromHtml("<h4>Days</h4>" + getDateInDays)); currentBirthdayMonth.setText(Html.fromHtml("<h4>Months</h4>" + getDateInMonths)); currentBirthdayYear.setText(Html.fromHtml("<h4>Years</h4>" + getDateInYears)); } private Period displayBirthdayResult(DateTime dateToday, DateTime birthdayDate){ Period dateDifferencePeriod = new Period(birthdayDate, dateToday, PeriodType.yearMonthDayTime()); return dateDifferencePeriod; } private DateTime convertToDateTime(String stringToConvert){ String[] newStringArray = convertStringToArray(stringToConvert); int year = Integer.parseInt(newStringArray[2].trim()); int day = Integer.parseInt(newStringArray[1].trim()); int month = Integer.parseInt(newStringArray[0].trim()); LocalDate mLocalDate = new LocalDate(year, month, day); DateTime firstDateTime = mLocalDate.toDateTime(LocalTime.fromDateFields(mLocalDate.toDate())); return firstDateTime; } private String[] convertStringToArray(String stringToConvert){ String[] newStringArray = stringToConvert.split("-"); return newStringArray; } public String setCurrentDateOnView() { final Calendar c = Calendar.getInstance(); year = c.get(Calendar.YEAR); month = c.get(Calendar.MONTH); day = c.get(Calendar.DAY_OF_MONTH); StringBuilder displayStringBuilder = new StringBuilder().append(month + 1).append("-").append(day).append("-").append(year).append(" "); return displayStringBuilder.toString(); } public void onActivityResult(int requestCode, int resultCode, Intent data) { //super.onActivityResult(requestCode, resultCode, data); switch(requestCode) { case DATE_PICKER_FRAGMENT: if (resultCode == Activity.RESULT_OK) { Bundle bundle = data.getExtras(); String resultDate = bundle.getString("RETURNED_DATE", "error"); todayDate.setText(resultDate); } break; case SECOND_DATE_PICKER_FRAGMENT: if (resultCode == Activity.RESULT_OK) { Bundle bundle = data.getExtras(); String resultDate = bundle.getString("RETURNED_DATE", "error"); yourBirthdayDate.setText(resultDate); } break; } } }
package com.inducesmile.completecalculatorapp; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.app.AppCompatActivity; import android.text.Html; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.DatePicker; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import org.joda.time.DateTime; import org.joda.time.LocalDate; import org.joda.time.LocalTime; import org.joda.time.Period; import org.joda.time.PeriodType; import org.joda.time.Weeks; import java.util.Calendar; public class AgeCalculatorFragment extends Fragment { private TextView todayDate; private ImageView todayDateSelector; private DatePicker todayDateDialog; private TextView yourBirthdayDate; private ImageView yourBirthdayDateSelector; private int year; private int month; private int day; public static final int DATE_PICKER_FRAGMENT = 1; public static final int SECOND_DATE_PICKER_FRAGMENT = 2; private TextView currentBirthdayYear; private TextView currentBirthdayMonth; private TextView currentBirthdayDay; private TextView nextBirthdayYear; private TextView nextBirthdayMonth; private TextView nextBirthdayDay; private TextView totalYearResult; private TextView totalMonthResult; private TextView totalWeekResult; private TextView totalDayResult; private TextView totalHourResult; private TextView totalMinuteResult; private TextView totalSecondResult; public AgeCalculatorFragment() { } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_age_calculator, container, false); ((AppCompatActivity) getActivity()).getSupportActionBar().show(); currentBirthdayYear = (TextView)view.findViewById(R.id.years_in_age); currentBirthdayMonth = (TextView)view.findViewById(R.id.months_in_age); currentBirthdayDay = (TextView)view.findViewById(R.id.days_in_age); nextBirthdayYear = (TextView)view.findViewById(R.id.years_in_birthday); nextBirthdayMonth = (TextView)view.findViewById(R.id.months_in_birthday); nextBirthdayDay = (TextView)view.findViewById(R.id.days_in_birthday); totalYearResult = (TextView)view.findViewById(R.id.total_years_result); totalMonthResult = (TextView)view.findViewById(R.id.total_months_result); totalWeekResult = (TextView)view.findViewById(R.id.total_weeks_result); totalDayResult = (TextView)view.findViewById(R.id.total_days_result); totalHourResult = (TextView)view.findViewById(R.id.total_hours_result); totalMinuteResult = (TextView)view.findViewById(R.id.total_minutes_result); totalSecondResult = (TextView)view.findViewById(R.id.total_seconds_result); todayDate = (TextView)view.findViewById(R.id.display_today_date); todayDateSelector = (ImageView)view.findViewById(R.id.set_date_icon); yourBirthdayDate = (TextView)view.findViewById(R.id.display_birthday_date); yourBirthdayDateSelector = (ImageView)view.findViewById(R.id.set_birthday_icon); Button calculateButton = (Button)view.findViewById(R.id.button_calculate); Button clearButton = (Button)view.findViewById(R.id.button_clear); todayDate.setText(setCurrentDateOnView()); yourBirthdayDate.setText(setCurrentDateOnView()); todayDateSelector.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { DatePickerFragment datePickerDialog = new DatePickerFragment(); datePickerDialog.setTargetFragment(AgeCalculatorFragment.this, DATE_PICKER_FRAGMENT); datePickerDialog.show(getFragmentManager(), "DATE TODAY"); } }); yourBirthdayDateSelector.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { DatePickerFragment datePickerDialog = new DatePickerFragment(); datePickerDialog.setTargetFragment(AgeCalculatorFragment.this, SECOND_DATE_PICKER_FRAGMENT); datePickerDialog.show(getFragmentManager(), "BIRTHDAY TODAY"); } }); calculateButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { String getTodayDate = todayDate.getText().toString(); String getBirthdayDate = yourBirthdayDate.getText().toString(); DateTime todayDateTime = convertToDateTime(getTodayDate); DateTime birthdayDateTime = convertToDateTime(getBirthdayDate); displayCurrentBirthday(todayDateTime, birthdayDateTime); displayNextBirthday(todayDateTime, birthdayDateTime); displayAgeAnalysis(todayDateTime, birthdayDateTime); } }); clearButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { currentBirthdayDay.setText(Html.fromHtml("<h4>Days</h4>")); currentBirthdayMonth.setText(Html.fromHtml("<h4>Months</h4>")); currentBirthdayYear.setText(Html.fromHtml("<h4>Years</h4>")); nextBirthdayDay.setText(Html.fromHtml("<h4>Days</h4>")); nextBirthdayMonth.setText(Html.fromHtml("<h4>Months</h4>")); nextBirthdayYear.setText(Html.fromHtml("<h4>Years</h4>")); totalDayResult.setText(""); totalWeekResult.setText(""); totalMonthResult.setText(""); totalYearResult.setText(""); totalHourResult.setText(""); totalMinuteResult.setText(""); totalSecondResult.setText(""); } }); return view; } private void displayAgeAnalysis(DateTime dateToday, DateTime birthdayDate){ Period dateDifferencePeriod = displayBirthdayResult(dateToday, birthdayDate); int getDateInDays = dateDifferencePeriod.getDays(); int getDateInWeeks = Weeks.weeksBetween(new DateTime(birthdayDate), new DateTime(dateToday)).getWeeks();; int getDateInMonths = dateDifferencePeriod.getMonths(); int getDateInYears = dateDifferencePeriod.getYears(); int mDay = getDateInWeeks * 7; int mMonth = getDateInMonths + (getDateInYears * 12); int hours = mDay * 24; int minutes = mDay * 24 * 60; int seconds = mDay * 24 * 60 * 60; totalDayResult.setText(Html.fromHtml(String.valueOf(mDay))); totalWeekResult.setText(Html.fromHtml(String.valueOf(getDateInWeeks))); totalMonthResult.setText(Html.fromHtml(String.valueOf(mMonth))); totalYearResult.setText(Html.fromHtml(String.valueOf(getDateInYears))); totalHourResult.setText(Html.fromHtml(String.valueOf(hours))); totalMinuteResult.setText(Html.fromHtml(String.valueOf(minutes))); totalSecondResult.setText(Html.fromHtml(String.valueOf(seconds))); } private void displayNextBirthday(DateTime dateToday, DateTime birthdayDate){ Calendar mCalendar = Calendar.getInstance(); int year = mCalendar.get(Calendar.YEAR); DateTime nextBirthday = birthdayDate.withYear(year); Toast.makeText(getActivity(), "Birthday " + nextBirthday.getYear(), Toast.LENGTH_LONG).show(); Period dateDifferencePeriod = displayBirthdayResult(nextBirthday, dateToday); int getDateInDays = dateDifferencePeriod.getDays(); int getDateInMonths = dateDifferencePeriod.getMonths(); int getDateInYears = dateDifferencePeriod.getYears(); nextBirthdayDay.setText(Html.fromHtml("<h4>Days</h4>" + getDateInDays)); nextBirthdayMonth.setText(Html.fromHtml("<h4>Months</h4>" + getDateInMonths)); nextBirthdayYear.setText(Html.fromHtml("<h4>Years</h4>" + getDateInYears)); } private void displayCurrentBirthday(DateTime dateToday, DateTime birthdayDate){ Period dateDifferencePeriod = displayBirthdayResult(dateToday, birthdayDate); int getDateInDays = dateDifferencePeriod.getDays(); int getDateInMonths = dateDifferencePeriod.getMonths(); int getDateInYears = dateDifferencePeriod.getYears(); currentBirthdayDay.setText(Html.fromHtml("<h4>Days</h4>" + getDateInDays)); currentBirthdayMonth.setText(Html.fromHtml("<h4>Months</h4>" + getDateInMonths)); currentBirthdayYear.setText(Html.fromHtml("<h4>Years</h4>" + getDateInYears)); } private Period displayBirthdayResult(DateTime dateToday, DateTime birthdayDate){ Period dateDifferencePeriod = new Period(birthdayDate, dateToday, PeriodType.yearMonthDayTime()); return dateDifferencePeriod; } private DateTime convertToDateTime(String stringToConvert){ String[] newStringArray = convertStringToArray(stringToConvert); int year = Integer.parseInt(newStringArray[2].trim()); int day = Integer.parseInt(newStringArray[1].trim()); int month = Integer.parseInt(newStringArray[0].trim()); LocalDate mLocalDate = new LocalDate(year, month, day); DateTime firstDateTime = mLocalDate.toDateTime(LocalTime.fromDateFields(mLocalDate.toDate())); return firstDateTime; } private String[] convertStringToArray(String stringToConvert){ String[] newStringArray = stringToConvert.split("-"); return newStringArray; } public String setCurrentDateOnView() { final Calendar c = Calendar.getInstance(); year = c.get(Calendar.YEAR); month = c.get(Calendar.MONTH); day = c.get(Calendar.DAY_OF_MONTH); StringBuilder displayStringBuilder = new StringBuilder().append(month + 1).append("-").append(day).append("-").append(year).append(" "); return displayStringBuilder.toString(); } public void onActivityResult(int requestCode, int resultCode, Intent data) { //super.onActivityResult(requestCode, resultCode, data); switch(requestCode) { case DATE_PICKER_FRAGMENT: if (resultCode == Activity.RESULT_OK) { Bundle bundle = data.getExtras(); String resultDate = bundle.getString("RETURNED_DATE", "error"); todayDate.setText(resultDate); } break; case SECOND_DATE_PICKER_FRAGMENT: if (resultCode == Activity.RESULT_OK) { Bundle bundle = data.getExtras(); String resultDate = bundle.getString("RETURNED_DATE", "error"); yourBirthdayDate.setText(resultDate); } break; } } }
ADD THE DATEPICKERFRAGMENT CLASS BELOW
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;
import java.util.Calendar;
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, year, month + 1, day);
}
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
int mYear = year;
int mMonth = monthOfYear + 1;
int mDay = dayOfMonth;
String selectedDate = dayOfMonth + " - " + mMonth + " - " + year;
//Toast.makeText(getActivity(), "Selected " + selectedDate, Toast.LENGTH_LONG).show();
Intent i = new Intent();
i.putExtra("RETURNED_DATE",selectedDate);
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, i);
}
}
No comments:
Post a Comment