Friday 6 November 2020

Changing DialogFragment Layouts while dialog is open

I have an Android Activity, from which I want to show a Dialog. It would probably be a custom DialogFragment. Now when the user clicks on specific buttons I want the layout's inside the dialog to change with the data from the previous DialogFragment and so that it would have an ability to also go back to previous Layout.

I dont think there is an easy way to change views inside of the same DialogFragment so what would be the best way to do this?

I have tried doing it in method onViewCreated and when a button is clicked, but nothing happens.

In my activity I call the fragment like this at the moment:

    FragmentManager fm = getSupportFragmentManager();
    NewDialog newDialog = NewDialog.newInstace(userId, loc, currentId);
    newDialog.setNewClickListener(new NewDialog.OnNewClickListener() {
        @Override
        public void onCancelClicked() {
            finishAdd();
        }

        @Override
        public void onAcceptClicked() {
            ...
        }
    });
    newDialog.show(fm, "new_frag");

And the fragment:

public class NewDeliveryPointDialog extends DialogFragment {
    private LayoutInflater inflater;
    private ViewGroup container;

    public NewDialog(){
    }

    public static NewDialog newInstace(){
        ...
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        this.container = container;
        return inflater.inflate(R.layout.dialog_layout_1, container);

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        saveButton.setOnClickListener(v -> {
                View.inflate(getContext(), R.layout.dialog_layout_2, container);
                view.invalidate();
                view.refreshDrawableState();
            }

        });
    }
}


from Changing DialogFragment Layouts while dialog is open

No comments:

Post a Comment