0

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

ybybyb
  • 1,385
  • 1
  • 12
  • 33
  • 2
    Does this answer your question: https://stackoverflow.com/a/3594216/4303296 ? – Beko Mar 08 '21 at 18:53
  • 1
    Does this answer your question? [View's getWidth() and getHeight() returns 0](https://stackoverflow.com/questions/3591784/views-getwidth-and-getheight-returns-0) – Henry Twist Mar 08 '21 at 19:03
  • @Beko I also saw the answer from the link. But I can't get the height for only one item... I've added the code – ybybyb Mar 08 '21 at 19:06
  • @Henry Twist I also saw the answer from the link. But I can't get the height for only one item... I've added the code – ybybyb Mar 08 '21 at 19:07
  • 1
    I've had problems with this before as well. Unfortunately I think there's nothing more to be said than what's already been told in the above links. I think you'll have to read through those answers and comments and try to figure out which one will work for you. Wish you good luck. – Beko Mar 08 '21 at 19:18
  • 1
    Looking at your updated code you seem to be inflating a layout but not using it? But trying to use its height? – Henry Twist Mar 08 '21 at 20:00
  • @Henry Twist Yes, I simply want to set the height of the dialog fragment by 5 heights of the corresponding layout – ybybyb Mar 08 '21 at 20:33

1 Answers1

3

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

Henry Twist
  • 5,666
  • 3
  • 19
  • 44
  • Wow thanks, it's almost solved. Almost means because there is a problem a little. i wanted to set the height of the item as many as 5, so I multiplied the value obtained with `getMeasuredHeight()` by `5`. However, the result is not all 5 items, but a bit cut off. In other words, the size of the dialog is slightly smaller. Do you know why? and i editted code – ybybyb Mar 09 '21 at 17:33
  • Your code doesn't actually seem to be using the measured height? – Henry Twist Mar 09 '21 at 17:56
  • It seems to be using height to be precise. However, it is not the height of five. The item comes out a bit cut off. i post picture – ybybyb Mar 09 '21 at 18:02