3

My DialogFragment class is invoked when I press a button on a activity. I want the date set through this DialogFragment to be displayed on the buttons text. FindViewById refuses to be recognized by the class.

I found a similar question but I'm unable to relate it to my case.

Code:

  1. Button which calls the Dialogue fragment:

    public void datepicker(View v) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager(), "datePicker"); 
    }
    
  2. Code for dialogue fragment:

    public class DatePickerFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceSateate) {
    
            final Calendar c = Calendar.getInstance();
                int year = c.get(Calendar.YEAR);
                int month = c.get(Calendar.MONTH);
                int day = c.get(Calendar.DAY_OF_MONTH);
    
                return new DatePickerDialog(getActivity(), this, year, month, day);
            }
    
            public void onDateSet(DatePicker view, int year, int month, int day) {
                // Do something with the date chosen
            }
        }
    }
    
Community
  • 1
  • 1
krtkush
  • 1,378
  • 4
  • 23
  • 46

4 Answers4

10

In the method in your DialogFragment that is in charge of being notified when the user sets the date, do this

Button activityButton = (Button)getActivity().findViewById(R.id.myButton);
activityButton.setText (myDate);
A--C
  • 36,351
  • 10
  • 106
  • 92
  • @K_K I used `getActivity()` which has a public `findViewById()`method. You need `getActivity()` since the button you're modifying is part of your Activity's layout, not your Fragment's. – A--C Dec 24 '12 at 19:36
  • This is not for de-couping between DialogFragment and Activity. What happen if the DialogFragment hosted by an activity that has no button with id is myButton? – LHA Jul 03 '14 at 20:41
  • @LocHa If the Activity doesn't have the button with this id, a `NullPointerException` will be thrown when you try using `activityButton`. For a Fragment attached to multiple Activities, it's easier to make a [Listener type interface](http://developer.android.com/training/basics/fragments/communicating.html). – A--C Jul 04 '14 at 00:07
3

Create Class for DatePicker Fragment:

    public class SelectDateFragment extends DialogFragment implements
                                     DatePickerDialog.OnDateSetListener {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

            Calendar now = Calendar.getInstance();
            year = now.get(Calendar.YEAR);
            month = now.get(Calendar.MONTH);
            day = now.get(Calendar.DATE);

         return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        orderDateEditText.setText(year + "-" + (month + 1) + "-" + day);
    }
}

Call this Fragment When clicked On ImageButton:

    /** Create DatePicker dialog on click of OrderDateEditText **/
   orderDatePickerButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            DialogFragment newFragment = new SelectDateFragment();
            newFragment.show(getFragmentManager(), "DatePicker");
        }
    });

In Month there is plus 1 because DatePicker default takes month from 0 to 11 for Jan to Dec.

Thank you..

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
1

here's a good pattern to follow:

  1. create a listener interface in your dialog to handle the relevant button presses in the dialog
  2. implement it in the starting activity
  3. when the user presses "okay" or "cancel", call the listener methods
  4. do whatever you need to do in the impl of the listener to update the activity's view

something like,

class MyDialogFragment extends DialogFragment implements DialogInterface.OnClickListener {
  static interface Listener {
    void onOkay(MyObject result);
    void onCancel();
  }
  ...

  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
    ...

    // set this as a listener for ok / cancel buttons
    return new AlertDialog.Builder(activity)...whatever...
        .setPositiveButton(R.string.ok, this)
        .setNegativeButton(R.string.cancel, this).create();
  }

  @Override
  public void onClick(DialogInterface dialog, int which) {
      if (which == DialogInterface.BUTTON_POSITIVE) {
          if (getActivity() instanceof Listener) {
              ((Listener)getActivity()).onOkay(...);
          }
      } else {
          if (getActivity() instanceof Listener) {
              ((Listener)getActivity()).onCancel();
          }
      }
  }
}

class MyActivity extends Activity implements MyDialogFragment.Listener {
  ...
  @Override
  public void onOkay(MyObject result) {
    // update activity view here with result
  }

  @Override
  public void onCancel() {
    // anything?
  }
}

if your case, the "result" passed into the onOkay() method would be the Date object picked by the user in the dialog.

Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
0

A Fragment is not a View. If you want to find a fragment, use findFragmentById from the FragmentManager.

K-ballo
  • 80,396
  • 20
  • 159
  • 169