How to create a custom dialog with two datepicker?

I was just starting to learn Android as a hobby, and I would like to create a dialogue with two datepicker

final Dialog dialog = new Dialog(this); dialog.setContentView(R.layout.data_picker_dialog); dialog.setTitle(R.string.date_period_picker); dialog.show(); return true; 

How to get selected values ​​from a dialog box? Is it possible to enable the OK / Cancel button in the dialog box?

Is there a library that has this functionality (start and end date / period selection)?

+6
source share
3 answers

It is better to read Dialogs and Pickers .

As for the implementation, you can have two buttons: one for displaying the date selection for the start date and the other for the end date.

Edit: if you really want to show 2 date selections in 1 dialog box, here is an example of how to do this. First, create your own XML layout.

/res/layout/custom_date_picker.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <DatePicker android:id="@+id/dpStartDate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:calendarViewShown="false" /> <DatePicker android:id="@+id/dpEndDate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:calendarViewShown="false" /> </LinearLayout> 

Next, use the above layout in the dialog box:

 // These variables will hold the date values later private int startYear, startMonth, startDay, endYear, endMonth, endDay; /** * Displays the start and end date picker dialog */ public void showDatePicker() { // Inflate your custom layout containing 2 DatePickers LayoutInflater inflater = (LayoutInflater) getLayoutInflater(); View customView = inflater.inflate(R.layout.custom_date_picker, null); // Define your date pickers final DatePicker dpStartDate = (DatePicker) customView.findViewById(R.id.dpStartDate); final DatePicker dpEndDate = (DatePicker) customView.findViewById(R.id.dpEndDate); // Build the dialog AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setView(customView); // Set the view of the dialog to your custom layout builder.setTitle("Select start and end date"); builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { startYear = dpStartDate.getYear(); startMonth = dpStartDate.getMonth(); startDay = dpStartDate.getDayOfMonth(); endYear = dpEndDate.getYear(); endMonth = dpEndDate.getMonth(); endDay = dpEndDate.getDayOfMonth(); dialog.dismiss(); }}); // Create and show the dialog builder.create().show(); } 

Finally, you can show this dialog just by calling showDatePicker() .

+12
source

Same for your layout

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <DatePicker android:id="@+id/datePicker1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <DatePicker android:id="@+id/datePicker2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> 
+1
source

You just want to create a date picker on your xml layout (data_picker_dialog). And get data from your id

0
source

Source: https://habr.com/ru/post/945575/


All Articles