Search code examples
javaandroiddialogandroid-dialogfragment

Setting a listener in Activity for DialogFragment


I am trying to implement a listener in my Activity on my DialogFragment( which has 3 numberPicker widget elements) which would be used to set values in textViews of the Activity and I don't want to make this Fragment class an inner class of Activity and set the textview in OnClickListener of the OK button as I will have to make my view static in that case which is not desirable. I know there is a listener onValueChange of the NumberPicker class, but how do I set a listener which gets values from the 3 picker elements. Any help to implement such a listener in the activity would be appreciated.

 public class PickerDialog extends DialogFragment {
        NumberPicker numberPicker;
        NumberPicker numberPicker2;
        NumberPicker numberPicker3;
        public static PickerDialog newInstance() {
            PickerDialog frag = new PickerDialog ();
            return frag;
        }


        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            View child = getActivity().getLayoutInflater().inflate(R.layout.dialog, null);
             numberPicker = child.findViewById(R.id.numberPicker1);
              numberPicker2 = child.findViewById(R.id.numberPicker2);
               numberPicker3 = child.findViewById(R.id.numberPicker3);

            numberPicker.setMinValue(0);
            numberPicker.setMaxValue(59);
            numberPicker3.setMinValue(0);
            numberPicker3.setMaxValue(59);

            numberPicker2.setMinValue(0);
            numberPicker2.setMaxValue(59);

            AlertDialog.Builder builder;
            builder = new AlertDialog.Builder(getActivity(), R.style.Theme_Material_Dialog_Alert);


            builder.setTitle("Choose Value");


            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    dismiss();
                }
            });

            builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dismiss();

                }
            });

            builder.setView(child);
            return builder.create();
        }




    }

Solution

  • You can create a Listener interface that is implemented in your Activity and used in DialogFragment through a reference to the Activity. Something like:

    public class MyActivity extends Activity implements NumberPickerListener {
        ...
        @Override
        public void onValuesPicked(int first, int second, int third) {
            //Do work here.
        }
        ...
    }
    

    The interface:

    interface NumberPickerLisener {
        void onValuesPicked(int first, int second, int third);
    }
    

    In your Fragment:

    public class PickerDialog extends DialogFragment {
    
        NumberPickerListener listener;
    
        ...
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            View child = getActivity().getLayoutInflater().inflate(R.layout.dialog, null);
            listener = (MyActivity)getActivity();
    
            ...
    
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    listener.onValuesPicked(numberPicker.getValue(), numberPicker2.getValue(), numberPicker3.getValue());
                    dismiss();
                }
            });
    
            ...
        }
    
    
    
    
    }