Diff with onCreateView and onCreateDialog in DialogFragment

I had a working DialogFragment that used an inner class to do a bunch of things on some objects, set menu icons, etc. When I went to Android Studio, I realized it was not correct and I was trying to change the inner class to be static.

So now I'm trying to use onCreateDialog for Google Docs , "doPositiveClick" and "doNegativeClick" so that the caller of MainActivity can do work on those objects instead of making a snippet.

What is confusing me right now is how to set the layout in the fragment - I can enter the title, message and buttons as such:

return new AlertDialog.Builder(getActivity())
                .setTitle(R.string.alert_title)
                .setMessage(R.string.alert_message)

                .setPositiveButton(R.string.set,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                ((MainActivity)getActivity()).doPositiveClick();
                            }
                        }
                )
                .setNegativeButton(R.string.cancel,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                ((MainActivity)getActivity()).doNegativeClick();
                            }
                        }
                )

      

But before I did a layout like:

        final EditText input = new EditText(MainActivity.this);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(20, 20);
        input.setText("5");
        input.setLayoutParams(lp);
        input.setRawInputType(Configuration.KEYBOARD_QWERTY);

      

The problem is where is this going in onCreateDialog? The google docs show how to set the text in the dialog's text box, but it's inside onCreateView ().

My misunderstanding is that google doc doesn't do both, i.e. it doesn't show how both, sets up custom items, AND sets up a positive / negative click in the calling MainActivity - or if it does, I'm sorry I can't see it right now.

So, can anyone make this clearer to me using the above onCreateDialog, how can I get the default editText field that accepts user input, and then return that input to doPositiveClick () for processing.

+3


source to share


2 answers


Sorry guys, I thought I had exhausted my searches, but right after I posted this I was able to fix it by placing the textbox / layout inside onCreateDialog in front of the Builder, and then doing setView () on that input as such:

        **LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(20, 20);
        final EditText input = new EditText(getActivity());
        input.setText("5");
        input.setLayoutParams(lp);
        input.setRawInputType(Configuration.KEYBOARD_QWERTY);**

        // Use the Builder class for convenient dialog construction
        return new AlertDialog.Builder(getActivity())
                .setTitle(R.string.alert_title)
                .setMessage(R.string.alert_message)
                **.setView(input)**
                .setPositiveButton(R.string.set,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                ((MainActivity)getActivity()).doPositiveClick();
                            }
                        }
                )

      



The only question is how to return the value after user input?

0


source


DialogFragment can be used in two ways: a dialog box or a view.

case1: use DialogFragment

as dialog. you need to implement onCreateDialog()

to get the dialog back. and then should show the dialogue like this. see example:

public static class MyAlertDialogFragment extends DialogFragment {

public static MyAlertDialogFragment newInstance(int title) {
    MyAlertDialogFragment frag = new MyAlertDialogFragment();
    Bundle args = new Bundle();
    args.putInt("title", title);
    frag.setArguments(args);
    return frag;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    int title = getArguments().getInt("title");

    return new AlertDialog.Builder(getActivity())
            .setIcon(R.drawable.alert_dialog_icon)
            .setTitle(title)
            .setPositiveButton(R.string.alert_dialog_ok,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        ((FragmentAlertDialog)getActivity()).doPositiveClick();
                    }
                }
            )
            .setNegativeButton(R.string.alert_dialog_cancel,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        ((FragmentAlertDialog)getActivity()).doNegativeClick();
                    }
                }
            )
            .create();
}

      

}

Create and display a dialog as follows. show that it doesn't matter whether it is implemented onCreateView()

or not.

    // Create the fragment and show it as a dialog.
DialogFragment newFragment = MyDialogFragment.newInstance();
newFragment.show(getFragmentManager(), "dialog");

      

case2: use as a view (this is not a dialog function). it's just a view. you should implement onCreateView()

and show the dialog like this:



public static class MyDialogFragment extends DialogFragment {
static MyDialogFragment newInstance() {
    return new MyDialogFragment();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.hello_world, container, false);
    View tv = v.findViewById(R.id.text);
    ((TextView)tv).setText("This is an instance of MyDialogFragment");
    return v;
}

      

}

and should show as below. just like the Fragment class. show that it doesn't matter whether it is implemented onCreateDialog()

or not.

FragmentTransaction ft = getFragmentManager().beginTransaction();
DialogFragment newFragment = MyDialogFragment.newInstance();
ft.add(R.id.embedded, newFragment);
ft.commit();

      

Summary: in design, you can implement onCreateView()

both onCreateDialog()

together and use the same source code with this DialogFragment

lifecycle. If the screen is small, use it DialogFragment

as a dialog. If the screen is large, use DialogFragment

as a view (same generic fragment class).

Note that use the correct way to show DialogFragment

to match with onCreateView()

and onCreateDialog()

to prevent an exception.

0


source







All Articles