Difficulty in using Datepicker

Asked

Viewed 131 times

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

1 answer

0

I thought of the following proposal: Use a DatePickerDialog in a custom class that will have an interface that will return a Celendar every time the user completes the date selection:

Datepickercalendar.java

public class DatePickerCalendar {
    private OnDateSetListener onDateSetListener;
    private Calendar caledarioPadrao;
    private Context contexto;

    public DatePickerCalendar(Calendar calendarioPadrao, Context context) {
        this.caledarioPadrao = calendarioPadrao;
        this.contexto = context;
        show();
    }

    public interface OnDateSetListener {
        void onDateSet(Calendar calendario);
    }

    public void setOnDateSetListener(OnDateSetListener onDateSetListener) {
        this.onDateSetListener = onDateSetListener;
    }

    private void show() {
        new DatePickerDialog(contexto, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                Calendar calendarioSelecionado = Calendar.getInstance();
                calendarioSelecionado.set(year, month, dayOfMonth);
                onDateSetListener.onDateSet(calendarioSelecionado);
            }
        }, caledarioPadrao.get(Calendar.YEAR), caledarioPadrao.get(Calendar.MONTH), caledarioPadrao.get(Calendar.DAY_OF_MONTH));
    }
}

In your Activity containing Textview

botaoData1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Calendar hoje = Calendar.getInstance();
        DatePickerCalendar pickerCalendar = new DatePickerCalendar(hoje, MainActivity.this);
        pickerCalendar.setOnDateSetListener(new DatePickerCalendar.OnDateSetListener() {
            @Override
            public void onDateSet(Calendar calendario) {
                TextView tv = findViewById(R.id.textView11);
                tv.setText(dateMillisToString(calendario.getTimeInMillis(), "dd/MM/yyyy"));
            }
        });            
    }
});

dateMillisToString

private String dateMillisToString(long millis, String pattern) {
    String s = "Data desconhecida";
    try  {
        DateFormat df = new SimpleDateFormat(pattern, Locale.getDefault());
        s = df.format(new Date(millis));
    } catch (Exception ignored) {}
    return s;
}

"And how to count the number of days between the two dates?"

One day has 86400000 milliseconds, you can catch the calendario.getTimeInMillis(), subtract by calendario2.getTimeInMillis() and multiply back by 86400000 to get the difference in days

Browser other questions tagged

You are not signed in. Login or sign up in order to post.