Solved it using a ViewTreeObserver
set in onCreateDialog
DialogFragment - Get width of dialog
-
28-06-2023 - |
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
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow