0

I create a class for custom dialog and I used PrettyDialog. I want an edittext on dialog. I use Inflater but Error: getActivity -> mHost:null. I tried create getTextDialogFragment() method, then getLayoutInflater error mHost null. What can I do fix that? TextDialogFragment:

 public class TextDialogFragment extends DialogFragment {
        private EditText edtDialog;
        public interface SingleChoiceListener{
            void onPositiveButtonClicked();
            void onNegativeButtonClicked();
        }
        TextDialogFragment.SingleChoiceListener mListener;


        @NonNull
        public PrettyDialog onCreateTextDialog(Context context) {
            PrettyDialog prettyDialog = new PrettyDialog(context);
            LayoutInflater inflater=getActivity().getLayoutInflater();
            View view=inflater.inflate(R.layout.dialog_text,null);
            prettyDialog.setContentView(view);
            prettyDialog.setMessage("sdf");
            prettyDialog.setIcon(R.drawable.question_icon);
            prettyDialog.setCanceledOnTouchOutside(false);
            prettyDialog.addButton("EVET", R.color.pdlg_color_white, R.color.pdlg_color_green, new PrettyDialogCallback() {
                @Override
                public void onClick() {
                    mListener.onPositiveButtonClicked();
                    prettyDialog.dismiss();
                }
            });
            prettyDialog.addButton("HAYIR", R.color.pdlg_color_white, R.color.pdlg_color_red, new PrettyDialogCallback() {
                @Override
                public void onClick() {
                    mListener.onNegativeButtonClicked();
                    prettyDialog.dismiss();
                }
            });
            edtDialog=view.findViewById(R.id.edtDialog);
            prettyDialog.show();
            return prettyDialog;
        }
        public void setListener(TextDialogFragment.SingleChoiceListener singleChoiceListener){
            mListener=singleChoiceListener;
        }
    }

this layout that I want add to dialog. Dialog has to work edittext+prettydialog:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:layout_margin="8dp">
    <EditText
        android:id="@+id/edtDialog"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@null"
        android:focusable="true"
        android:hint="Giriş yapınız"
        android:gravity="center"
        android:imeOptions="actionDone"
        android:inputType="text"
        android:textColor="@android:color/black"
        android:textColorHint="@android:color/darker_gray"
        android:textSize="40sp">

    </EditText>
</RelativeLayout>
CEng
  • 53
  • 7
  • Move everything in your `onCreateTextDialog()` method to `DialogFragment`'s `onCreateDialog()` method. You can use `getActivity()` there in place of your `Context` parameter. I'm not quite sure what `onCreateTextDialog()` is for, but it kinda suggests that you might not be using `DialogFragment` correctly. – Mike M. Dec 18 '19 at 08:20
  • @MikeM. thank you for ur support. I use BaseNavigator so I have to use context parameter.. I create oncreatedialog but I can't use with context – CEng Dec 18 '19 at 08:27
  • I don't know what `BaseNavigator` is, nor why it would require that you pass your own `Context` there, but as it is currently, your `TextDialogFragment` is not really being used as a `DialogFragment`. If you `show()` the `PrettyDialog` returned from `onCreateTextDialog()`, then you've just got a plain old `Dialog`. If instead you try to `show()` an instance of `TextDialogFragment`, it's going to be an empty `Dialog`, and not the `PrettyDialog` you're setting up in that method. – Mike M. Dec 18 '19 at 08:34

1 Answers1

0

Try below code

View view=LayoutInflater.from(context).inflate(R.layout.dialog_text,null);

Instead of

LayoutInflater inflater=getActivity().getLayoutInflater();
View view=inflater.inflate(R.layout.dialog_text,null);

I hope this can help you!

Thank You.

Hardik Talaviya
  • 1,396
  • 5
  • 18