DialogFragment not able to return value... ??

IvanHarrisIvanHarris USMember ✭✭✭

I have a fragment within a FragmentActivity for my tabs. Within my tabs I can swipe the screen to move throughout the tabs better. Within one of the fragments/tabs, I have a button that opens up the DialogFragment (DatePickerDialog). I got that to work. What I have not been able to figure out is getting the return value of the date when I click "Set" within the dialogfragment. Im trying to pass that value to a TextView so the user can see what date they just selected/set.

I used the CustomDialogFragmentSample (https://github.com/t9mike/CustomDialogFragmentSample) as a starting point.

Below is my DialogFragment:

        private DateTime date;
        private int[] hours = { 1, 0 };
        private int[] minutes = { 2, 0 };
        string timeCapture = "";
        string dateCapture = "";
        View v;
       public override Android.Views.View OnCreateView(Android.Views.LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
            v = inflater.Inflate(Resource.Layout.LayoutTest, container, false);

            return v;
        public override Dialog OnCreateDialog(Bundle savedInstanceState)
            var settings = PreferenceManager.GetDefaultSharedPreferences(Activity);
            int claimID_Dialog = settings.GetInt("claimID_Dialog", 0);
            date = DateTime.Today;

            switch (claimID_Dialog)
                case 0:
                    return new DatePickerDialog(Activity, OnDateSet, date.Year, date.Month - 1, date.Day);
                //case 1:
                  //  return new TimePickerDialog(Activity, TimePickerCallbackScheduled, hours[0], minutes[0], true);
                //case 2:
                  //  return new TimePickerDialog(Activity, TimePickerCallbackActual, hours[1], minutes[1], true);
            return null;

        private void UpdateDeptDate()
            TextView txtTest_Date= v.FindViewById<TextView>(Resource.Id.txtTest_Date);
            txtTest_Date.Text = dateCapture;

        private void OnDateSet(object sender, DatePickerDialog.DateSetEventArgs e)
            this.date = e.Date;
            dateCapture = date.Month.ToString("00") + "/" + date.Day.ToString("00") + "/" + date.Year.ToString("0000");

Within my fragment, I use this code to open the dialog box:

                var transaction = FragmentManager.BeginTransaction();
                var dialog = new Test_DialogFrag();
                dialog.Show(transaction, "dialog");

As you could see above, I have the layout: v = inflater.Inflate(Resource.Layout.LayoutTest, container, false) <-- that is in my main Fragment too. I thought that I could call the same layout and set the TextView within it (TextView txtTest_Date= v.FindViewById<TextView>(Resource.Id.txtTest_Date)).

Anything Im missing?


  • StevaSteva RSMember ✭✭✭

    I'm not 100% sure what you are doing here but I think I know what the problem is. You are creating , a dialog fragment within a fragment and trying to update those values within the fragment. Doing it like this is not possible as far as I know.

    What you need to do is move that communication to containing activity. You can implement callback interface in your date picker fragment like this:

    private IOnDateSelectedListener _callback;
    public interface IOnDateSelectedListener
        void OnDateSelected(string dateCapture)
    public override void OnAttach(Activity activity)
            _callback = (IOnDateSelected) activity;
            throw new ClassCastException(activity.ToString() + "must implement IOnDateListener");
    //Implementation of IOnDateSetListener
    public void OnDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
        //your code to generate date capture

    Make your UpdateDeptDate method public and add string dateCapture as parameter. Implement this interface in your activity and inside OnDateSelected method just find tab fragment and call UpdateDeptDate(dateCapture).

    So communication between fragments is always done within containing activity.

Sign In or Register to comment.