I'm trying to determine the height of a TextView
before it is drawn. I'm using the following code to do that:
TextView textView = (TextView) findViewById(R.id.textview);
textView.setText("TEST");
int widthSpec = MeasureSpec.makeMeasureSpec(LayoutParams.MATCH_PARENT, MeasureSpec.EXACTLY);
int heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
textView.measure(widthSpec, heightSpec);
System.out.println("MeasuredHeight: " + textView.getMeasuredHeight());
The output is MeasuredHeight: 28
. Nothing wrong with that.
However, when I give the TextView
a long text string, so wrapping occurs, it still gives the height of a single line instead of two:
(...)
textView.setText("TESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTEST");
(...)
The output is MeasuredHeight: 28
, where I would expect MeasuredHeight: 56
.
Why doesn't it give me the right value and how can I achieve the right value?