How to calculate the necessary height of EditText to fully display a string?

bsnqt

Active Member
Licensed User
Longtime User
Question: How I can calculate the necessary height of an EditText view to fully display a given string?

Description of the situation:

I have one string MyString, and one view EditText1. The width of this EditText1 is fixed, for example, equal to 100%x.

My task now is to display MyString in the EditText1 fully, i.e MyString can be read by the user without need of using the scrolling of the editText1. So EditText1's height shoud not be too short for MyString. At the same time the height of EditText1 should be also not too high (otherwise if MyString is short, the "empty" area in the EditText1 is ugly).

In summary: In one word, because MyString can change the contents (so its length change every time it displays), so I need to calculate how much the height of EditText1 should be in order to fit (fully display) MyString.

B4X:
Dim LineHeigth as Float
Dim CvsText As Canvas
Dim NumberofLines As Int
Dim TextLength As Float

LineHeight = CvsText.MeasureStringHeight("Ag", Typeface.DEFAULT ,13) + LineSpace
TextLength = CvsText.MeasureStringWidth(MyString, Typeface.DEFAULT, 13)
NumberofLines = TextLength/EditText1.Width

Then the height of EditText1 will be calculated as follows:

B4X:
Dim DisplayHeight as Float

DisplayHeigth = NumberofLines * LineHeight

Result / Issue and Question: The calculation above is correct only when MyString does NOT contain any line break, but in the reality myString does contain a lot of line breaks such as CRLF and Chr(10). The problem is I can not know how the line breaks to be inserted into my calculation, and I don't know how to get the LineSpace.

Any advice or any link, guideline, sample... please?

Thank you.
 
Last edited:
Top