3

Is there any way to get the linespacing of a TextView in Android? I try to find fontMetrics of the Paint of the TextView and do like this:

tv1.getPaint().getFontMetrics(pfm);
float fontTop = pfm.top;
float fontBottom = pfm.bottom;
System.out.println("Line Space >> " + (fontBottom - fontTop));

But it seems that result is the same until I change font size of the TextView. So how can I get the linespacing of a TextView?

Onik
  • 19,396
  • 14
  • 68
  • 91
hasanghaforian
  • 13,858
  • 11
  • 76
  • 167

3 Answers3

3

Linespacing of TextView is

textView.getPaint().getFontSpacing() * textView.getLineSpacingMultiplier() + textView.getLineSpacingExtra();

Note getLineSpacingMultiplier() and getLineSpacingExtra() methods are available since API 16+.

Onik
  • 19,396
  • 14
  • 68
  • 91
1

getLineSpacingExtra (); Gets the line spacing extra space

tv1.getLineSpacingExtra ()

textView.setLineSpacing() or from xml you can use android:lineSpacingExtra for setting the extra space.

Zohra Khan
  • 5,182
  • 4
  • 25
  • 34
0
    try {
        Field mSpacingAddField = TextView.class.getDeclaredField("mSpacingAdd");
        Field mSpacingMultField = TextView.class.getDeclaredField("mSpacingMult");
        mSpacingAddField.setAccessible(true);
        mSpacingMultField.setAccessible(true);
        mSpacingAdd = mSpacingAddField.getFloat(textView);
        mSpacingMult = mSpacingMultField.getFloat(textView);
    } catch (Exception e) {
        e.printStackTrace();
    }