Open a calendar in a Alertdialog and save the chosen date

Asked

Viewed 272 times

1

I’m creating an app and I need that in one component EditText, when I click on it, a calendar appears (I thought to put in a AlertDialog because of the add and cancel buttons, but if anyone has a better idea I’d appreciate it if you share).

The person would choose a date, the date would be on EditText in the form of "dd/mm/yyyy", and that date would be saved later in the database in that format.

I have a layout with a CalendarView, the class that accompanies the layout, and this class, when clicked on a date, would lead to another class that would capture that date:

Intent intent = getIntent();
Long dateSelected = intent.getLongExtra("dataLongMiliseconds", 0);
Date date = new Date(dateSelected);

The problem is that I am not able to program this in a Fragment where it contains a form where the EditText date.

1 answer

3


Do the following in the fragment your editText creates a Listener for it and initializes the calendar as follows:

   seuEditText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

         final Calendar calendar = Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH);
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            DatePickerDialog datePickerDialog = new DatePickerDialog(SuaActivity.this,
                    new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                            calendar.set(year, month, day);
                            String format = "dd/MM/yyyy";
                            SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.ENGLISH);
                            Date date;

                            try {
                                date = sdf.parse(sdf.format(calendar.getTime()));
                                String dayS = new SimpleDateFormat("dd", Locale.ENGLISH).format(date);
                                String monthS = new SimpleDateFormat("MM", Locale.ENGLISH).format(date);
                                String yearS = new SimpleDateFormat("yyyy", Locale.ENGLISH).format(date);

                                seuEditText.setText((dayS + "/" + monthS + "/" + yearS));
                            } catch (ParseException ignored) {

                            }
                        }
                    }, year, month, day);
            datePickerDialog.show();
            datePickerDialog.getDatePicker();
        }

            }
        });

And to recover text in edittext:

seuEditText.getText().toString();

I hope this helps you. Any doubt just call me.

UPDATE:

To change the color theme of datepickerdialog do the following in the res/values/Styles folder, create a style:

 <style name="DialogTheme" parent="Theme.AppCompat.Light">
        <item name="colorAccent">#F1C46B</item>
    </style>

And then in the code passed above, when you initialize a new Datepickerdialog, put this theme after the context, and before the Ondatesetlistener(), like this:

        DatePickerDialog datePickerDialog = new  DatePickerDialog(SuaActivity.this, 
             R.style.DialogTheme, new DatePickerDialog.OnDateSetListener() {
        .
        .
        .

The Example of this theme changes the default color of the Dialog, but it is also possible to change other colors, for example:

<style name="DialogTheme" parent="Theme.AppCompat.Light">
    <item name="colorAccent">#ff6d00</item>
    <item name="colorControlActivated">#33691e</item>
    <item name="android:selectableItemBackgroundBorderless">#8E61DF</item>
    <item name="colorControlHighlight">#d50000</item>
</style>
  • It worked, buddy?

  • It worked right buddy, thank you very much! I just have one question, like, is there any way I can customize (like the color for example) the calendar that appears to me? Or program an option for people to customize?

  • 1

    I updated my answer, it’s simple to change some colors. It would also give the user to change the colors, but there is more comprehensive programming, and based on this my example is possible to do.

  • Thank you very much! :)

Browser other questions tagged

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