B4A Library [B4X] LineUtils v1.2 - get required height for multiline TextView/label with variable line spacing


Based on code by Erel for StringUtils function MeasureMultilineTextHeight
StringUtils function calculates the height for multiplier value 1 only.
With this function you can calculate height with variable line spacing.

  • LineUtils
    • getMultilineHeight (TextView As android.widget.TextView, Text As java.lang.CharSequence, multi As float) As int
      Returns the required height in order to show the given text in a label with varying line spacing.
      This can be used to show dynamic text in a label.
      Note that the label must first be added to its parent and only then its height can be set.

      Usage: getMultiLineHeight( TextView as android.widget.TextView , Text as CharSequence, multiplier as float)
      Multiplier is Line Spacing Multiplier, where 1 is normal, 2 is double line spacing so on...

      Dim Label1 As Label
      Label1.Text = "this is a long sentence, and we need to " _
      & "know the height required in order To show it completely."
      Label1.TextSize = 20
      Activity.AddView(Label1, 10dip, 10dip, 200dip, 30dip)
      Dim LineS As LineUtils
      Label1.Height = LineS.getMultilineHeight(Label1, Label1.Text, 1.5)

Used Code:
    public int getMultilineHeight(TextView TextView, CharSequence Text, float multi) {
        StaticLayout sl = new StaticLayout(Text, TextView.getPaint(),
                TextView.getLayoutParams().width - TextView.getPaddingLeft() - TextView.getPaddingRight(),
                Alignment.ALIGN_NORMAL, multi, 0 , true);
        return sl.getLineTop(sl.getLineCount());


  • LineUtils.zip
    1.8 KB · Views: 301
Last edited:


Active Member
Licensed User
Does it support B4J and B4i?
Its a simple inline java function taken from stringutils, passed with additional parameters, so technically it should work with B4J and B4i both, however I have not tested it on either.