4
I’m studying things for a project here and I’m trying to use Datepicker, I managed to implement normally, but I want to use two in the same Activity, putting an initial date and an end date, put two buttons that use the same Datepickerfragment.java, my xml is 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 at the end make the account of the days between the two dates and show in the third Textview. I tried to use a switch to change each one of the Textview, and that’s where I need help, what will the switch look like? How to differentiate the return of each button? And how to count the number of days between the two dates ? Follow the button code:
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");
}
});
}
Code of Datepickerfragment:
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);
} }
This library already has Date and Time Picker, and the implementation is simple. https://github.com/wdullaer/MaterialDateTimePicker
– Weslley Barbosa