How to set the current date on DatePickerDialog when it opens? I have searched for several sources but none of them are in Xamarin Android (mostly xamarin.forms, android studio java). I am trying to figure out the syntaxes on xamarin.forms but it is hard to compare it to xamarin android.
I am coding from a fragment
private int year, month, day;
Button eventpickdateButton;
TextInputLayout eventdatesetText;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.Inflate(Resource.Layout.newevent, container, false);
this.Dialog.SetCanceledOnTouchOutside(false);
eventpickdateButton = (Button)view.FindViewById(Resource.Id.eventpickdateButton);
eventdatesetText = (TextInputLayout)view.FindViewById(Resource.Id.eventdatesetText);
eventpickdateButton.Click += EventpickdateButton_Click;
return view;
}
private void EventpickdateButton_Click(object sender, EventArgs e)
{
DatePickerDialog datePickerDialog = new DatePickerDialog(this.Activity, this, year, month, day);
datePickerDialog.Show();
}
public void OnDateSet(DatePicker view, int year, int month, int dayOfMonth)
{
this.year = year;
this.month = month + 1;
this.day = dayOfMonth;
eventdatesetText.EditText.Text = month + "/" + dayOfMonth + "/" + year;
}
and for some reason, my month does not add + 1 when picking a date. Whenever I pick January as a month, it shows "0/dd/yyyy" even though I added 1 to its value. Why is that? Thank you for future answers. I appreciate it!
I don't see you define eventdatesetText in the provided code, you can try to change your code to:
eventdatesetText.EditText.Text = this.month + "/" + this.day+ "/" + this.year;
or
eventdatesetText.EditText.Text = month+1 + "/" + dayOfMonth + "/" + year;
In Xamarin.Android's official documentation about the use of DatePickerDialog, you can refer to the following documentation for more details: Android Date Picker | Microsoft