Measuring TextView with compound drawable returns wrong value
Asked Answered
R

0

6

I want to know a view's size before hand. I measure its size this way.

public static int[] measureSize(ViewGroup parent, int layoutId)
{
    View view = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
    final int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    view.measure(spec, spec);
    final int width = view.getMeasuredWidth(); // ? 0, not include compound drawable size and relative padding. WHY
    final int height = view.getMeasuredHeight();
    return new int[]{width, height};
}

This is the layout file:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableStart="?android:listChoiceIndicatorSingle"
    android:drawablePadding="20dp"/>

The question: the measured view width does always not include the compound drawable size and its related padding. If I don't set text, the measured width is always 0. If set a not empty value, the width is only for the text only. Why? How can I get the correct size?

Thanks in advance.

Resource answered 8/4, 2020 at 11:26 Comment(2)
pls check this #24430929. Hope this help!Aeolipile
@ChuongLeVan Thanks but it does not help.Resource

© 2022 - 2024 — McMap. All rights reserved.