Search code examples
androidandroid-layoutdialogandroid-alertdialogdialogfragment

Custom AlertDialog?


I'm trying to create Custom AlertDialog that gives me the full details of an item in listview when it clicks, but when I inflate the layout it won't give me proper measurements of the layouts I have created here's what I mean:

how I want it to be seen:

how it appear with onCreateDialog

Listener

Sizes of layouts in the XML code


Solution

  • For inflating your dialog to the full view you must add this code to the onActivityCreated() or can be done it onCreate Dialog() but after dialog creation. So it is would be efficient to use this in onActivityCreated() method

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(getLayoutId(), container, false);
    }
    
    
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Dialog dialog = getDialog();
        if (dialog != null) {
                dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
            }
        }
    }
    

    Hoe this will help you.