jacky0karl
jacky0karl

Reputation: 21

getMeasuredHeight() of Textview returned wrong value

I call all the mentioned methods right after the onMeasure() of TextView's parent layout returned, like the following:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int mh = mTextView.getMeasuredHeight();
    int h = mTextView.getLineCount() * mTextView.getLineHeight();
}

mh is wrong and shorter than h, does anyone know why, thanks?

Upvotes: 0

Views: 813

Answers (1)

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

// Try this way,hope this will help you to solve your problem...

  mTextView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    int mh = mTextView.getMeasuredHeight();
                    int h = mTextView.getLineCount() * mTextView.getLineHeight();
                }
  });

Upvotes: 1

Related Questions