2

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. enter image description here

What I expect, but doesn't work, because the getMeasuredHeight returns the wrong height: enter image description here

If I manually put \n feed, so there is no auto-wrap getMeasuredHeight returns the right height and everything works as expected. enter image description here

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.

ForJ9
  • 735
  • 8
  • 24
  • when you need to calculate the height of textView. can you please specify scenario so I can help you – Jitesh Mohite Nov 02 '16 at 16:53
  • I've a recyclerview which items expand depending on the height and the amount of text in the textview, so the expanded height will fit the text. – ForJ9 Nov 02 '16 at 16:58
  • how is it possible to get the actually height of the textview with automatically wrapped text? means what exactly automatically mean by – Jitesh Mohite Nov 02 '16 at 17:00
  • If you put a very long text in a textview, Android automatically put the long text in multiple lines. – ForJ9 Nov 02 '16 at 17:02
  • so, textview size increases or if it will go next line then you want to get the height of textView ? Can you attach screenshoot if possible – Jitesh Mohite Nov 02 '16 at 17:05
  • Not sure of what you are asking, it is your width that is resizing, not your height. Wouldn't it be constant? If I am mistaken and you really do need the height, then it is probably your approach. Get the measured height of the parent view. See if that helps. – portfoliobuilder Nov 02 '16 at 17:27
  • I've added some pictures, maybe it will be more clear. – ForJ9 Nov 02 '16 at 17:37

2 Answers2

0

For long texts, TextView takes a certain amount of time to render. So before that we are calculating the height of that TextView

Use the following code:

view.setText("Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test ...");

view.getViewTreeObserver()
    .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Toast.makeText(getApplicationContext(), "Height :- " + view.getMeasuredHeight(), Toast.LENGTH_SHORT).show();
        }
    });

Please refer to How to find the Text Area(Height/Width) of TextView programmatically in android

If you want to see exact behavior of TextView then create a CustomTextView and see how their lifecycle behaves.

Please let me know if it dosen't work for you.

muetzenflo
  • 5,653
  • 4
  • 41
  • 82
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
0

I dont know whether you have figured out a solution for this, the solution is when you are measuring height with definite width specified you need to be very careful with the width (because the calculation is very precise) if you are not getting desired height that means you are missing something in your spec,

example is below

val widthMeasureSpec =
      View.MeasureSpec.makeMeasureSpec(ceil(context.resources.getDimension(R.dimen.text_width)).toInt(), View.MeasureSpec.EXACTLY)
    val heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED).  

in the above code what you pass matter for the width parameter,

  • you need pass the exact width that you have passed for your textview
  • you need to pass measure spec as View.MeasureSpec.EXACTLY in my case
Preethi Rao
  • 5,117
  • 1
  • 16
  • 29