Java – gets the height of the textview
I have some text to put in textview I did it with settext()
Now I need to find the number of lines or height of text in textview I try to use getHeight (), but it always returns 0
Anyway, you want to get the height of the text in the textview?
Solution
As John pointed out, you won't get height immediately after settext I'm not sure how much use of getHeight () on textview will help you The height of the view depends not only on the height of the visible text in the view, but also on the view group / layout where the textview is located If the view group tells textview to maximize the height, getHeight () will be nowhere to see if you wait until the text is rendered
I see many ways to do this:
>Subclass textview and override the onsizechanged method There, call super onsizechanged, get the number of lines in textview through getlinecount(), and get the height of each line using getlineheight() This may or may not be better than using getHeight (), depending on your layout or something like that. > Do not use the textviews dimension Use textview Getpaint() gets textviews paint and calculates the width and height
Rectangular boundary; paint. getTextBounds(text,text.length(),bounds);
You will now have dimensions in range You can now use paint Breaktext to see how much text you can fit on a line It may be too cumbersome and there is no guarantee (for my untrained eyes) that the logic used by textview is the same