I'm studying things for a project here and I'm trying to use DatePicker, I could implement it normally, but I want to use two in the same activity, putting a start date and an end date, I put two buttons that use the same DatePickerFragment.java, my xml looks like this:
<TextView
android:id="@+id/textoData1ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="59dp"
android:text="Data inicial"
android:textSize="30dp" />
<Button
android:id="@+id/botaoData1ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="118dp"
android:text="Abrir texto inicial" />
<TextView
android:id="@+id/textoData2ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="202dp"
android:text="Data final"
android:textSize="30dp" />
<Button
android:id="@+id/botaoData2ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="268dp"
android:text="Abrir data final" />
<TextView
android:id="@+id/textoDataFinalID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="212dp"
android:text="Dias entre elas"
android:textSize="30dp" />
My idea is on each button to open a DatePicker and change the TextView to that date, and in the end make the count of days between the two dates and show in the third TextView. I tried to use a switch to change each one a TextView, and that's where I need help, what will the switch look at? How to differentiate the return of each button? And how do you count the amount of days between the two dates? Here is the code for the buttons:
botaoData1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment datePicker1 = new DatePickerFragment();
datePicker1.show(getSupportFragmentManager(), "date picker");
}
});
botaoData2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment datePicker2 = new DatePickerFragment();
datePicker2.show(getSupportFragmentManager(), "date picker");
}
});
}
DatePickerFragment code:
public class DatePickerFragment extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
int ano = c.get(Calendar.YEAR);
int mes = c.get(Calendar.MONTH);
int dia = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(),(DatePickerDialog.OnDateSetListener)getActivity(), ano,mes,dia);
} }