I've created an expandable cardview, which contains a title, subtitle and a detail textview.
Now I want to get the height dynamically depending on the height of the text in a textview with wrap_content
. So I used the .measure
method on my view in the onClickListener:
holder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.getDetailText().measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Log.i("Project", ""+ holder.getDetailText().getMeasuredHeight());
}
});
I get a height of 57 with this text:
holder.getDetailText().setText("Test Test Test Test");
If I now add a longer text in there which automatically wraps to multiple lines I get the same height of 57
holder.getDetailText().setText("Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test ...");
As soon as I put a text with new line feed in there everything works as expected which results in a height of 155
holder.getDetailText().setText("Test\nTest\nTest");
So how is it possible to get the actually height of the textview with automatically wrapped text?
Update:
This is a very long text which is automatically wrapped, but I get the height of 57, so it will only display the first line, because the getMeasuredHeight
only returns this (57) height.
What I expect, but doesn't work, because the getMeasuredHeight
returns the wrong height:
If I manually put \n
feed, so there is no auto-wrap getMeasuredHeight
returns the right height and everything works as expected.
It seems that the .measure
ignores the width, because even if I put there a constant value the height returned by getMeasuredHeight
wont't change.