8

can I know what is the difference between lineHeight and lineSpacingExtra in android xml. I tried to compare both, but I got the different result (I need the lineHeight function, however it is only supported in API 28).

Here is part of my code:

left:

android:textSize="14sp"

android:lineSpacingExtra="6dp"

right:

android:textSize="14sp"

android:lineHeight="20dp"

Result:

enter image description here

Any solution for this? Thank you.

hallz12
  • 629
  • 2
  • 9
  • 15

4 Answers4

5

You mentioned you want to set lineHeight in pre-API 28, an alternative approach would be to just set a small lineSpacingExtra / lineSpacingMultiplier value (as you have shown). Otherwise, there are many solutions to setting a line height yourself in this related question.

I'll briefly cover the differences below, with my summary and the official documentation.

android:lineHeight is the total height of each line. This includes the text, as well as any padding on the top and bottom.

Explicit height between lines of text. If set, this will override the values set for lineSpacingExtra and lineSpacingMultiplier.

android:lineSpacingExtra is the additional spacing added after each line of text (except the last one).

Extra spacing between lines of text. The value will not be applied for the last line of text.

Finally, you may be interested in android:lineSpacingMultiplier. This is similar to lineSpacingExtra, but with a multiplier value of the current height (e.g. 1.2) instead of a fixed value.

Extra spacing between lines of text, as a multiplier. The value will not be applied for the last line of text.

Further information (besides the included quotas) is available in the TextView documentation.

Jake Lee
  • 7,549
  • 8
  • 45
  • 86
  • Hi, thanks for your answers. But, I don't quite understand about "Explicit height between lines of text". By the way, you mentioned that I can use my alternative approach in pre-API 28, but I got a different result for that. Any idea or calculation to get the same result as using lineHeight? – hallz12 Jan 15 '19 at 11:37
  • Simply the "gap" between lines of text! There's quite a few answers here already for calculating / setting it yourself in pre-API 28. – Jake Lee Jan 15 '19 at 11:37
  • I have tried those solutions, however the result is different from the lineHeight provided in API 28. – hallz12 Jan 15 '19 at 11:41
  • 1
    If you really need specific line heights, there [are very good libraries](https://github.com/Actinarium/Aligned) specifically for this! Alternatively, you could mark this answer as solved (since you asked the difference between two XML parameters), then make a new one explicitly explaining your new question? – Jake Lee Jan 15 '19 at 11:45
5

MaterialTextView (MTV) includes the ability to set the android:lineHeight.

And if you use the app: prefix, it is backwards compatible until SDK-Version 21.

Btw., under some circumstances (i believe its depending on your style, but am not sure about it), standard TextViews are automatically replaced with MTVs. (Check your LayoutInspector, you might already use them without knowing it)

In case you're still wondering about the differences, there's an excellent talk by the android team about text in general; which should solve your question at ~16:50.

m.reiter
  • 1,796
  • 2
  • 11
  • 31
2
android:lineHeight

is used to specify the total height for single line of text.

android:lineSpacingExtra

is used to add extra spacing between 2 lines of text in addition to the default line height used by android.

In order to replicate lineHeight functionality with API level < 28, set the lineSpacingMultiplier to 0 and set the lineSpacingExtra to the lineHeight that you want to use

android:lineSpacingMultiplier="0"
android:lineSpacingExtra="<enter line height value here in dp>"
1

android:lineHeight defines Explicit height between lines of text. while android:lineSpacingExtra defines Extra spacing between lines of text you can read more android_documentation_for_text_arributes