I have an EditText and a button for the DatePickerFragment but the syntaxes are not correct of showing the DatePickerDialog and OnCreateDialog method.
This is a fragment wherein I have a TextInputLayout/EditText to be accessed on click in-order to show the DatePickerDialog. This is my code but ShowDialog() is error and is suggesting ShowsDialog() which is an error as well.
OnCreateDialog error says "CS0115: 'AddEventFragment.OnCreateDialog(int)': no suitable method found to override
namespace AdamsonsEDApp.Fragments
{
public class AddEventFragment : Android.Support.V4.App.DialogFragment, Android.App.DatePickerDialog.IOnDateSetListener
{
private int year, month, day;
TextInputLayout eventdatesetTextt;
Button eventpickdateButton;
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.Inflate(Resource.Layout.newevent, container, false);
this.Dialog.SetCanceledOnTouchOutside(false);
eventdatesetText = (TextInputLayout)view.FindViewById(Resource.Id.eventdatesetText)
eventpickdateButton = (Button)view.FindViewById(Resource.Id.eventpickdateButton);
eventpickdateButton.Click = EventpickdateButton_Click;
return view;
}
private void EventpickdateButton_Click(object sender, EventArgs e)
{
int DatePickerDialogID = 1;
ShowDialog(DatePickerDialogID);
}
public override Dialog OnCreateDialog(int id)
{
if (id == 1)
{
return new Android.App.DatePickerDialog(this.Activity, this, year, month, day);
}
return null;
}
public void OnDateSet(DatePicker view, int year, int month, int dayOfMonth)
{
this.year = year;
this.month = month;
this.day = dayOfMonth;
eventdatesetText.EditText.Text = year "/" month "/" dayOfMonth;
}
For the click event I tried:
private void EventpickdateButton_Click(object sender, EventArgs e)
{
int DatePickerDialogID = 1;
**this.Activity.ShowDialog(DatePickerDialogID);**
}
It has no error but it says this this.Activity.ShowDialog(DatePickerDialogID); is obsolete: 'deprecated'
What is the right syntax when calling a DatePickerDialog from a Fragment? Thank you.
CodePudding user response:
You can refer to this doc. I test the code official doc provided, and there are also many obsolete
problems. However, you can add attribute and other things to fix it.
Here is the Changed code:
[Obsolete]
public class DatePickerFragment : DialogFragment,
DatePickerDialog.IOnDateSetListener
{
// TAG can be any string of your choice.
public static readonly string TAG = "X:" typeof(DatePickerFragment).Name.ToUpper();
// Initialize this value to prevent NullReferenceExceptions.
Action<DateTime> _dateSelectedHandler = delegate { };
public static DatePickerFragment NewInstance(Action<DateTime> onDateSelected)
{
DatePickerFragment frag = new DatePickerFragment();
frag._dateSelectedHandler = onDateSelected;
return frag;
}
[Obsolete]
public override Dialog OnCreateDialog(Bundle savedInstanceState)
{
DateTime currently = DateTime.Now;
DatePickerDialog dialog = new DatePickerDialog(Activity,
this,
currently.Year,
currently.Month - 1,
currently.Day);
return dialog;
}
public void OnDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
// Note: monthOfYear is a value between 0 and 11, not 1 and 12!
DateTime selectedDate = new DateTime(year, monthOfYear 1, dayOfMonth);
Log.Debug(TAG, selectedDate.ToLongDateString());
_dateSelectedHandler(selectedDate);
}
}
MainActivity.cs:
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
TextView _dateDisplay;
Button _dateSelectButton;
[Obsolete]
#pragma warning disable CS0809 // Obsolete member overrides non-obsolete member
protected override void OnCreate(Bundle savedInstanceState)
#pragma warning restore CS0809 // Obsolete member overrides non-obsolete member
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
_dateDisplay = FindViewById<TextView>(Resource.Id.date_display);
_dateSelectButton = FindViewById<Button>(Resource.Id.date_select_button);
_dateSelectButton.Click = _dateSelectButton_Click;
}
[Obsolete]
private void _dateSelectButton_Click(object sender, EventArgs e)
{
DatePickerFragment frag = DatePickerFragment.NewInstance(delegate (DateTime time)
{
_dateDisplay.Text = time.ToLongDateString();
});
frag.Show(FragmentManager, DatePickerFragment.TAG);
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
Wish it could be helpful to you.