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
  • 17,358
  • 12
  • 67
  • 87
hasanghaforian
  • 13,498
  • 9
  • 73
  • 157

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
  • 17,358
  • 12
  • 67
  • 87
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,024
  • 4
  • 24
  • 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();
    }