I want to set the height of the dialog fragment
by finding the height of the layout.
That's five heights
in the view.
I couldn't get the height of the layout using getHeight()
.
I for this I used getMeasuredHeight(
). (getHeight()
only returned 0.)
But getMeasuredHeight()
doesn't seem to return the exact value either.
I tried to set it to a height of 5
times this layout, but the actual height is
Less than 5 heights are set. (Please refer to the photo)
Why is it like this?
writing_comment_xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".fragment.WritingCommentDialogFragment">
<EditText
android:id="@+id/comment_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:gravity="center_vertical"
android:drawableLeft="@drawable/ic_bullet_point"
android:drawablePadding="5dp"
android:layout_marginHorizontal="10dp"
android:background="@null"
android:textSize="15sp"
android:inputType="text"
android:maxLines="1"
android:maxLength="22"
android:imeOptions="actionNext"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
</EditText>
</androidx.constraintlayout.widget.ConstraintLayout>
EDITED
DialogFragment.java
public class WritingCommentDialogFragment extends DialogFragment implements CommentModel.EditInputListener {
OnBackPressedCallback callback;
LinearLayout commentContainer; // input layout
private final List<Comment> comments = new ArrayList<>();
private LayoutInflater layoutInflater;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_writing_comment_dialog, container, false);
bindViews(view);
addCommentItem();
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setDialogSize();
}
private void bindViews(View v) {
commentContainer = v.findViewById(R.id.container);
layoutInflater = LayoutInflater.from(getContext());
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setCanceledOnTouchOutside(false);
return dialog;
}
private void setDialogSize() {
View v = LayoutInflater.from(getContext()).inflate(R.layout.writing_comment_item, null, false);
new Handler().post(() -> {
int size = v.getHeight() * 5;
getDialog().getWindow().setLayout(1000, size);
});
}
}
ADDED
fragment_writing_comment_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".fragment.WritingCommentDialogFragment">
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
I think the underlying issue is that the dialog will slip inside another ViewGroup called the decorView which has some padding that is shrinking the dialog. Here is a simplified version of your code that resizing the dialog's window appropriately so the dialog is the size you want. Comments in the code explain things a little more.
public class WritingCommentDialogFragment extends DialogFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setWindowSize();
View view = inflater.inflate(R.layout.fragment_writing_comment_dialog, container, false);
return view;
}
private void setWindowSize() {
int[] widthHeight = getDialogSize();
Window window = getDialog().getWindow();
// Our dialog will go inside the decorView which may have padding. So, the window has
// to be the width of our dialog plus the padding.
View decorView = window.getDecorView();
int horizontalPadding = decorView.getPaddingStart() + decorView.getPaddingEnd();
int verticalPadding = decorView.getPaddingTop() + decorView.getPaddingBottom();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(window.getAttributes());
lp.width = widthHeight[0] + horizontalPadding;
lp.height = widthHeight[1] + verticalPadding;
window.setAttributes(lp);
}
private int[] getDialogSize() {
View v = LayoutInflater.from(getContext())
.inflate(R.layout.writing_comment_item,
(ConstraintLayout) getView(), // This is the parent.
false); // Don't attach the view to the parent.
// Measure one of the five child views to be added. The width is match_parent and the
// height is wrap_content.
v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
int height = v.getMeasuredHeight();
return new int[]{DIALOG_WIDTH, height * 5};
}
private static final int DIALOG_WIDTH = 1000; // Maybe should be dp?
}