diff --git a/app/src/main/java/fr/imt_atlantique/myfirstapplication/DatePickerFragment.java b/app/src/main/java/fr/imt_atlantique/myfirstapplication/DatePickerFragment.java new file mode 100644 index 0000000000000000000000000000000000000000..f41d57642174e59cdb3c6484103bdb3667bd9bcf --- /dev/null +++ b/app/src/main/java/fr/imt_atlantique/myfirstapplication/DatePickerFragment.java @@ -0,0 +1,94 @@ +package fr.imt_atlantique.myfirstapplication; + +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; + +import androidx.fragment.app.Fragment; + +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.DatePicker; + +import java.util.Date; + +public class DatePickerFragment extends Fragment { + private static final String ARG_DATE = "date"; + + private Date mDate; + private DatePicker datePicker; + private Button validateDateButton; + private Button cancelButton; + private onUserPickingDateListener mListener; + + public DatePickerFragment() { + // Required empty public constructor + } + + public interface onUserPickingDateListener { + void onUserPickingDate(Date date); + } + + public interface onDateSubmittedListener { + void onDateSubmitted(int day, int month, int year); + } + + public interface onDateCancelledListener { + void onDateCancelled(); + } + + public static DatePickerFragment newInstance(Date date) { + DatePickerFragment fragment = new DatePickerFragment(); + Bundle args = new Bundle(); + args.putLong(ARG_DATE, date.getTime()); + fragment.setArguments(args); + return fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + if (getArguments() != null) { + mDate = new Date(getArguments().getLong(ARG_DATE)); + } + } + + @Override + public void onAttach(Context context) { + super.onAttach(context); + if (context instanceof onUserPickingDateListener) { + mListener = (onUserPickingDateListener) context; + } else { + throw new RuntimeException(context.toString() + + " must implement onUserPickingDateListener"); + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + View view = inflater.inflate(R.layout.fragment_date_picker, container, false); + + DatePicker datePicker = view.findViewById(R.id.datePicker); + + validateDateButton = view.findViewById(R.id.validateDateButton); + validateDateButton.setOnClickListener(() -> { + int day = datePicker.getDayOfMonth(); + int month = datePicker.getMonth() + 1; + int year = datePicker.getYear(); + + mListener.onDateSubmittedListener(day, month, year); + }); + + cancelButton = view.findViewById(R.id.cancel_button); + cancelButton.setOnClickListener(() -> { + mListener.onDateCancelledListener(); + }); + + + return view; + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_date_picker.xml b/app/src/main/res/layout/fragment_date_picker.xml new file mode 100644 index 0000000000000000000000000000000000000000..f215b9480c5c1d38255a16a06b2a298d85b691d6 --- /dev/null +++ b/app/src/main/res/layout/fragment_date_picker.xml @@ -0,0 +1,52 @@ +<?xml version="1.0" encoding="utf-8"?> +<androidx.constraintlayout.widget.ConstraintLayout 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" + tools:context=".DatePickerFragment"> + + <LinearLayout + android:layout_width="match_parent" + android:layout_height="match_parent" + android:layout_marginStart="10dp" + android:layout_marginTop="100dp" + android:layout_marginEnd="10dp" + android:layout_marginBottom="100dp" + android:orientation="vertical" + android:background="@drawable/rounded_card"> + + <DatePicker + android:id="@+id/datePicker" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_gravity="center" + android:layout_marginBottom="16dp"/> + + + <LinearLayout + android:layout_width="match_parent" + android:layout_height="match_parent" + android:orientation="horizontal"> + + <Button + android:id="@+id/validateDateButton" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_weight="1" + android:background="@color/green" + android:textColor="@color/white" + android:text="@string/validate" /> + + <Button + android:id="@+id/cancel_button" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_weight="1" + android:background="@color/red" + android:textColor="@color/white" + android:text="@string/cancel" /> + </LinearLayout> + + </LinearLayout> + +</androidx.constraintlayout.widget.ConstraintLayout> \ No newline at end of file