Android TextView padding between lines

Viewed 178989

I have a TextView which displays a long text. I want to give some space between lines like in CSS with line-height property. How can I do it?

10 Answers

This supplemental answer shows the effect of changing the line spacing.

enter image description here

You can set the multiplier and/or extra spacing with

textView.setLineSpacing(float add, float mult)

Or you can get the values with

int lineHeight = textView.getLineHeight();
float add = tvSampleText.getLineSpacingExtra();          // API 16+
float mult = tvSampleText.getLineSpacingMultiplier();    // API 16+

where the formula is

lineHeight = fontMetricsLineHeight * mult + add

The default multiplier is 1 and the default extra spacing is 0.

Adding android:lineSpacingMultiplier="0.8" can make the line spacing to 80%.

You can use lineSpacingExtra and lineSpacingMultiplier in your XML file.

lineSpacingExtra add extra spacing between lines of text of TextView

<TextView
    android:lineSpacingExtra="4dp" />

lineSpacingMultiplier works as scale factor for height of line space:

<TextView
    android:lineSpacingMultiplier="1.2" />

In other words, each line height will be height * multiplier + extra.

You can use 2 attrs


1. lineSpacingExtra: it use for dp spacing

android:lineSpacingExtra="4dp"

2. lineSpacingMultiplie: it use for relative scale

android:lineSpacingMultiplier="0.8"

As an extended answer to above solutions

Remember you can add <item name="android:lineHeight">16sp</item> for directly setting the line heights but as per the docs above line works like this -

Explicit height between lines of text. If set, this will override the values set for lineSpacingExtra and lineSpacingMultiplier.
        <attr name="lineHeight" format="dimension" />

So be sure to use either lineSpacingExtra & lineSpacingMultiplier or lineHeight and not both.

As of 16/11/2021, I use this line to increase the line space height:

android:lineHeight="25dp"

For me the other answers weren't helpful because maybe they updated the attribute and quite a lot of stuff changed in this version.

Related