RUVIDEO
Поделитесь видео 🙏

Datepicker android - Create datepicker dialog in android studio

📁 Обучение 👁️ 19 📅 02.12.2023

learn how to create datepicker Dialog in #Android. this video show you all of the steps involved to create #datepicker in #android studio

Hey guys welcome to #Androchunk. In this channel you learn about android tutorials and build android application so make sure you Subscribe.

? Subscribe for more: https://www.youtube.com/channel/UCnRqW8jM_q31ZUOtDT4wVig?sub_confirmation=1

✔ Full source code:- https://github.com/Androchunk/datepickerDialog

----------------------------------------------
? Adding a fragment

To add a fragment for the date picker, create a blank fragment (datepickerFragment) without a layout XML, and without factory methods or interface callbacks:

? Extending DialogFragment for the datepicker dialog: The next step is to create a standard picker with a listener. Follow these steps:

1. Edit the datepickerFragment class definition to extend DialogFragment, and implement DatePickerDialog.OnDateSetListener to create a standard date picker with a listener:

public class datepickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
...
}

2. Android Studio also shows a red light bulb icon in the left margin, prompting you to implement methods. Click the icon and, with ondateSet already selected and the "Insert @Override" option checked, click OK to create the empty onDateSet() method.

3. Replace onCreateView() with onCreateDialog() :

? Setting the defaults and returning the picker: To set the current date in the picker and return it as an object you can use, follow these steps:

1. Add the following code to the onCreateDialog() method to set the current date for the picker:

// Use the current date as the default date in the picker.
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);

2. Add the following statement to the end of the method to create a new instance of the date picker and return it:

return new DatePickerDialog(getActivity(), this, year, month, day);

? Processing the user's picker choice: The ondateSet() method is automatically called when the user makes a selection in the date picker, so you can use this method to do something with the chosen date. Follow these steps:

1. To make the code more readable, change the ondateSet() method's parameters from int i , int i1 to int hour, int minute :

Public void onDateSet(DatePicker datePicker, int year, int month, int day);

2. create public interface datepickerListner in datepickerFragment

public interface DatePickerListener {
void onDateSet(DatePicker datePicker, int year, int month, int day);
}

3. implement datepickerDialog.OndateSetListener in datepickerFragment

public class datepickerFragment extends DialogFragment implements datepickerDialog.OndateSetListener {

4. Override onAttach method and add following.

@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mListener = (DatePickerListener) context;
} catch (Exception e) {
throw new ClassCastException(getActivity().toString() + " Must Implements DatePickerListener");
}
}

5. set ondateSet method to listener

@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
mListener.onDateSet(datePicker, i, i1, i2);
}

6. Open MainActivity and implements datepickerFragment.datepickerListener.
add the ondateSet() method signature that takes the datepicker, year,month, and day arguments:

@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
Calendar cal=Calendar.getInstance();
cal.set(Calendar.YEAR,year);
cal.set(Calendar.MONTH,month);
cal.set(Calendar.DAY_OF_MONTH,day);
String date= DateFormat.getDateInstance().format(cal.getTime());
tvDisplayDate.setText(date);

}

? Displaying the datepicker dialog

To display datepicker dialog goto MainActivity() And add following on button click event.

DialogFragment datepickerFragment = new datepickerFragment();
datepickerFragment.setCancelable(false);
datepickerFragment.show(getSupportFragmentManager(), "datepicker");

----------------------------------------------
If you have any questions, comments or suggestions about this video scripts please leave them in the comments.

? Official documentation:

Pickers: https://developer.android.com/guide/topics/ui/controls/pickers

datepickerDialog: https://developer.android.com/reference/android/app/datePickerDialog.html

Developer-training.gitbooks: https://google-developer-training.gitbooks.io/android-developer-fundamentals-course-practicals/content/
----------------------------------------------
Music Credits:
Title: Dawn
Artist: Sappheiros

Что делает видео по-настоящему запоминающимся? Наверное, та самая атмосфера, которая заставляет забыть о времени. Когда вы заходите на RUVIDEO, чтобы посмотреть онлайн «Datepicker android - Create datepicker dialog in android studio», вы рассчитываете на нечто большее, чем просто загрузку плеера. И мы это понимаем. Контент такого уровня заслуживает того, чтобы его смотрели в HD 1080, без дрожания картинки и бесконечного буферизации.

Честно говоря, Rutube сегодня — это кладезь уникальных находок, которые часто теряются в общем шуме. Мы же вытаскиваем на поверхность самое интересное. Будь то динамичный экшн, глубокий разбор темы от любимого автора или просто уютное видео для настроения — всё это доступно здесь бесплатно и без лишних формальностей. Никаких «заполните анкету, чтобы продолжить». Только вы, ваш экран и качественный поток.

Если вас зацепило это видео, не забудьте взглянуть на похожие материалы в блоке справа. Мы откалибровали наши алгоритмы так, чтобы они подбирали контент не просто «по тегам», а по настроению и смыслу. Ведь в конечном итоге, онлайн-кинотеатр — это не склад файлов, а место, где каждый вечер можно найти свою историю. Приятного вам отдыха на RUVIDEO!

Видео взято из открытых источников Rutube. Если вы правообладатель, обратитесь к первоисточнику.