1

How do you change the current string of a text on a AlertDialog after you click a button that selects a date from a CalendarPickerFragment.

This the AlertDialog

    public class FragmentInicio extends Fragment{
       private static String date;

        public void addDate(){

        View view = (LayoutInflater.from(getActivity())).inflate(R.layout.alert_dialog,null);

      AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
      alertDialog.setView(view);

      final TextView date_Text = view.findViewById(R.id.dateEditText);
      final ImageButton dateButton= view.findViewById(R.id.tareaFechaButton);
      dateButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            CalendarPickerFragment newFragment = new CalendarPickerFragment();
            newFragment.show(getFragmentManager(), "DatePicker");

        }
    });

      date_Text.setText(date);

      alertDialog.setTitle("New Date");
      alertDialog.setCancelable(true).setPositiveButton("Save", new 
  DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
           //Some stuff
        }
    });

    alertDialog.setNegativeButton("Cancel", new 
   DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
  Dialog dialog = alertDialog.create();
dialog.show();

}

public static void displaydate(int year, int monthOfYear, int dayOfMonth) {
    date = String.format("%02d", dayOfMonth) + "/" + String.format("%02d", monthOfYear + 1) + "/" +
            String.format("%02d", year);
    Log.d("DTAG", "date: "+date);
}

CalendarPickerFragment

public class CalendarPickerFragment extends DialogFragment implements  DatePickerDialog.OnDateSetListener {

private CalendarPickerFragment.OnDateReceiveCallBack mListener;
private Context context;

public interface OnDateReceiveCallBack {
    public void onDateReceive(int dd ,int mm, int yy);
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    this.context = context;

    try {
        mListener = (CalendarPickerFragment.OnDateReceiveCallBack) context;
    } catch (ClassCastException e) {
        throw new ClassCastException(context.toString() + " must implement OnDateSetListener");
    }
}

public CalendarPickerFragment(){
}



@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    final Calendar cal = Calendar.getInstance();
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH);
    int day = cal.get(Calendar.DAY_OF_MONTH);

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

@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
    Log.d(TAG, i + "/" + "/" + i1 + "/" + i2);
    mListener.onDateReceive(i,i1,i2);
}
  • if you can provide your alert_dialog xml file too – Fahed Yasin May 01 '18 at 15:43
  • in date picker there is an interface called onDateSelectedListner. which returns the date in its call back function onDateSet this is the method where you can change the edit text value – Adeel Turk May 01 '18 at 15:45
  • @AdeelTurk Could you explain me how? I already tried that way – Alfonso Briceño May 01 '18 at 16:05
  • @AlfonsoBriceño can you share CalendarPickerFragment code then i can tell you the exact answer – Adeel Turk May 01 '18 at 16:07
  • and what i get from your question is that you wanna show the date selected or any other text in edittext on date selection.. – Adeel Turk May 01 '18 at 16:12
  • @AdeelTurk Okay I added it to the main post and yes thats correct I want to show the selected date in the Edit Text of the AlertDialog – Alfonso Briceño May 01 '18 at 16:15
  • Are you starting a `DialogFragment` from an `AlertDialog`? And you want to use the date from the "CalendarPickerFragment" and insert it in the `EditText` of your `AlertDialog`? What happens when you start the "CalendarPickerFragment" does the `AlertDialog` stay available after the date is selected from the "CalendarPickerFragment" ? – Barns May 01 '18 at 16:34
  • @Barns Yes it stays available but without the date, the date is set when I open the dialog again. My problem is that I want to set the date on the current Alert Dialog – Alfonso Briceño May 01 '18 at 16:38
  • I can't really see your user scenario, but wouldn't it be easier just to create a `DialogFragment` with the calendar and `EditText` and just start that? Why the detour over an `AlertDialog` ?? Your Custom `AlertDialog` appears to have only the two views `Button` and `EditText`. – Barns May 01 '18 at 16:46
  • @Barns What you cant see? Is a relatively simple problem I think. But Im going to take your advice anyways and do it with a DialogFragment maybe that solves my problem. – Alfonso Briceño May 01 '18 at 16:59
  • @AlfonsoBriceño your actvity have implemted CalendarPickerFragment.OnDateReceiveCallBack interface right ? – Adeel Turk May 01 '18 at 17:06
  • @AdeelTurk Yes I get the date correctly on my fragment, the problem is I want to get that date and set it on the current running alert dialog. – Alfonso Briceño May 01 '18 at 17:11
  • of why dont you implement the listener on aler dialog and pass it to the Calednar Fragment .. let me make a sudo for you – Adeel Turk May 01 '18 at 17:17
  • @AlfonsoBriceño check my answer and let me know if it worked. I m pretty sure it should work – Adeel Turk May 01 '18 at 17:31
  • Simple? I look at it from the UX. You have a `Fragment` "FragmentInicio" that when you trigger `addDate()` you display an `AlertDialog` which appears to have the sole function of showing the user a date in the `EditText` "date_Text" and allowing the user to change that date by opening another dialog "CalendarPickerFragment" from which the user can select a date, which is then returned to the `AlertDialog` and then what? Bubble that date back up to "FragmentInicio"? It appears to me that the `AlertDialog` has no significant roll in the UX except to disrupt the experience. – Barns May 01 '18 at 18:28
  • @Barns That's not the whole code obviously, I was just putting the essential code for finding the solution – Alfonso Briceño May 02 '18 at 00:22

1 Answers1

2

Ok so you can use the listener you have already craeted in your alertDialog Following should be your CalendarPickerFragment

    public class CalendarPickerFragment extends DialogFragment implements  DatePickerDialog.OnDateSetListener {

        private CalendarPickerFragment.OnDateReceiveCallBack mListener;
        private Context context;

        private static CalendarPickerFragment.OnDateReceiveCallBack mListenerForAlertDialog;

        public interface OnDateReceiveCallBack {
            public void onDateReceive(int dd, int mm, int yy);
        }

        public static CalendarPickerFragment getInstance(CalendarPickerFragment.OnDateReceiveCallBack callback) {
            mListenerForAlertDialog = callback;
            return new CalendarPickerFragment();
        }

        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            this.context = context;

            try {
                mListener = (CalendarPickerFragment.OnDateReceiveCallBack) context;
            } catch (ClassCastException e) {
                throw new ClassCastException(context.toString() + " must implement OnDateSetListener");
            }
        }

        public CalendarPickerFragment() {
        }


        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {

            final Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH);
            int day = cal.get(Calendar.DAY_OF_MONTH);

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

        @Override
        public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
            Log.d("tag", i + "/" + "/" + i1 + "/" + i2);
            mListener.onDateReceive(i, i1, i2);
            mListenerForAlertDialog.onDateReceive(i, i1, i2);
        }

    }

Now initialise this fragmnet as follows

CalendarPickerFragment newFragment =  CalendarPickerFragment.getInstance(new CalendarPickerFragment.OnDateReceiveCallBack() {
                @Override
                public void onDateReceive(int dd, int mm, int yy) {
                    // boom you wil get the call back on each date selection here
                    //eidtTextOfALertDialog.setText("whatEver")
                }
            });
            newFragment.show(getFragmentManager(), "DatePicker");
Adeel Turk
  • 897
  • 8
  • 23