Kristofer
Kristofer

Reputation: 817

How to settext that is in a fragment from a dialog fragment?

So I have a button that I want to settext as the date selected in my Dialog Fragment. I have tried using SharedPreferences but it wouldnt update unless fragment is restarted. I tried to look at the fragments lifecycle but I don't think it works the same way for a Dialog Fragment. SO my question is how do I set the text of the fragment from a dialog fragment initiated by the fragment

This is my code for my Dialog Fragment

public class DatePickerDialogTheme extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

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

        return new DatePickerDialog(getActivity(), R.style.DatePickerTheme,this,year,month,day);
    }

    @Override
    public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {

        String string = (i1 + 1) + "/" +i2 + "/" + i;
        SharedPreferences.Editor editor = getActivity().getSharedPreferences("itemInfo", Context.MODE_PRIVATE).edit();
        editor.putString("Birthday", string);
        editor.apply();


    }
}

And this is what I'm trying to change the text of in my fragment

mButton = view.findViewById(R.id.openCalendar);
mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogFragment dialogFragment = new DatePickerDialogTheme();
                dialogFragment.show(getActivity().getFragmentManager(), "Theme");
            }
        });

I also tried changing the text during onResume but still wouldnt work. I don't think it calls onResume when a DialogFragment opens up.

Upvotes: 0

Views: 343

Answers (3)

Ashutosh Sagar
Ashutosh Sagar

Reputation: 1009

Pass your TextView object in the constructor of that fragment ad you are initializing it and then set the desired text. This could be a quick way around for what you are trying to do.

public class DatePickerDialogTheme extends DialogFragment implements DatePickerDialog.OnDateSetListener {
private TextView tv;

DatePickerDialogTheme(TextView tv){
    this.tv = tv;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

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

    return new DatePickerDialog(getActivity(), R.style.DatePickerTheme,this,year,month,day);
}

@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {

    String string = (i1 + 1) + "/" +i2 + "/" + i;
    tv.setText(string);
   }

}

And for the other part -

tv= parentViewOfTextView.findViewById(R.id.textView);
mButton = view.findViewById(R.id.openCalendar);
mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DialogFragment dialogFragment = new DatePickerDialogTheme(tv);
            dialogFragment.show(getActivity().getFragmentManager(), "Theme");
        }
    });

Upvotes: 0

Kristofer
Kristofer

Reputation: 817

Thanks for the other answers but this worked out best for me.

I just used

Button button = getActivity().findViewById(R.id.calendarView);
button.setText(string);

in my dialog fragment and it works.

Upvotes: 1

Tam Huynh
Tam Huynh

Reputation: 2477

Here is a proper way to do it:

Define an interface for receiving result (Notice that I use getChildFragmentManager to access the manager for child fragment, don't use call getFragmentManager

public interface DatePickerResultHandler {
    void onDatePicked(DatePickerDialogTheme dialog, int year, int month, int day);
}

Make the outer Fragment implement the interface to receive the result

public class TestFragment extends Fragment implements DatePickerResultHandler {
    private Button mButton;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_test, container, false);
        mButton = view.findViewById(R.id.test_btn);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogFragment dialogFragment = new DatePickerDialogTheme();
                dialogFragment.show(getChildFragmentManager(), "Theme");
            }
        });

        return view;
    }

    @Override
    public void onDatePicked(DatePickerDialogTheme dialog, int year, int month, int day) {
        String string = (year + 1) + "/" + month + "/" + day;
        mButton.setText(string);
    }
}

Find the DatePickerResultHandler in DatePickerDialogTheme. First try to cast it with the Activity or Context if you attach this dialog to the Activity only. If you attach it to parent fragment, cast it to getParentFragment

public class DatePickerDialogTheme extends DialogFragment implements DatePickerDialog.OnDateSetListener {
    private boolean isAttach;
    private DatePickerResultHandler mResultHandler;

    @Override
    public void onAttach(Context context) {
        if (!isAttach) {
            findResultHandler(context);
            findResultHandler(getParentFragment());

            isAttach = true;
        }

        super.onAttach(context);
    }

    @Override
    public void onAttach(Activity activity) {
        if (!isAttach) {
            findResultHandler(activity);
            findResultHandler(getParentFragment());

            isAttach = true;
        }

        super.onAttach(activity);
    }

    private void findResultHandler(Object parent) {
        if (mResultHandler == null && parent instanceof DatePickerResultHandler)
            mResultHandler = (DatePickerResultHandler) parent;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

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

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

    @Override
    public void onDateSet(DatePicker datePicker, int year, int month, int day) {
        if (mResultHandler != null)
            mResultHandler.onDatePicked(this, year, month, day);

        dismiss();
    }
}

Upvotes: 0

Related Questions