Search code examples
androidandroid-viewappcompatdialogfragment

getHeight() doesn't get the height of the layout


I'm going to use dialog fragments.

There is an item called writing_comment_item.xml in the dialogfragment, and I want to set the height of 5 items.

I used the inflate() function, I used getHeight() to get the layout.

But as a result of debugging, the height is 0 no matter how much.

I tried in onCreate(). Also tried onCreateView() as well.

But the result was the same

What should I do?

writing_comment_item.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="300dp"
        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>

WCDialogFragment.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;
    }

    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;
    }

    @Override
    public void onResume() {
        super.onResume();
        setDialogSize();
    }

    private void setDialogSize() {
        View v = LayoutInflater.from(getContext()).inflate(R.layout.writing_comment_item, null, false);
        int h = v.getHeight() * 5;

        getDialog().getWindow().setLayout(1000, h);
    }
}

EDITED

private void setDialogSize() {
        View v = LayoutInflater.from(getContext()).inflate(R.layout.writing_comment_item, null, false);
        v.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        v.getMeasuredHeight();
        int h = v.getMeasuredHeight() * 5;
        getDialog().getWindow().setLayout(1000, h);
    }

ADDED PIC enter image description here


Solution

  • The mistake here is trying to get the height of a view that hasn't been added to the layout hierarchy. When you call

    inflate(R.layout.writing_comment_item, null, false);
    

    with attachToRoot being false, you are specifying that you don't want it to be added to the hierarchy yet so the system never measures the view.

    You could instead call View.measure(int, int) yourself and then use View.getMeasuredHeight(). This question covers that in a bit more detail - Measuring a view before rendering it