Question

I'm trying to get the width of a DialogFragment on runtime, I've tried two methods which failed.

First: inside onCreateDialog after the dialog has been created

public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    ...
    ...
    AlertDialog dialog =  builder.create();
    width = dialog.getWindow().getAttributes().width;
    return dialog;
}   

width was set to -1

Second:

public void onStart() {
    width = getDialog().getWindow().getAttributes().width;
}

width was set to -2

Was it helpful?

Solution

Solved it using a ViewTreeObserver set in onCreateDialog

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top