Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DialogFragment - Get width of dialog

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

like image 788
Calc Avatar asked Nov 01 '22 02:11

Calc


1 Answers

Solved it using a ViewTreeObserver set in onCreateDialog

like image 180
Calc Avatar answered Nov 12 '22 23:11

Calc