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?
– Murillo Comino
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?
– David
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.
– Murillo Comino
Thank you very much! :)
– David