Android TextView padding between lines

AndroidTextview

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

You can use lineSpacingExtra and lineSpacingMultiplier in your XML file.

Solution 2 - Android

If you want padding between text, try LineSpacingExtra="10sp"

<TextView
    android:layout_width="match_parent"
    android:layout_height="180dp"
    android:lineSpacingExtra="10sp"/>

Solution 3 - Android

you can look into android:lineSpacingExtra and apply it to your XML

Additional Info is on this page

or the related method public void setLineSpacing (float add, float mult)

Additional Info here

Solution 4 - Android

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.

Solution 5 - Android

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

Solution 6 - Android

You can use TextView.setLineSpacing(n,m) function.

Solution 7 - Android

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.

Solution 8 - Android

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"

Solution 9 - Android

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.

Solution 10 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Question&#199;ağatay G&#252;rt&#252;rkView Question on Stackoverflow
Solution 1 - AndroidRomain GuyView Answer on Stackoverflow
Solution 2 - Androidchanu panwarView Answer on Stackoverflow
Solution 3 - AndroidsealzView Answer on Stackoverflow
Solution 4 - AndroidSuragchView Answer on Stackoverflow
Solution 5 - AndroidAjitsenView Answer on Stackoverflow
Solution 6 - AndroidVishnu HaridasView Answer on Stackoverflow
Solution 7 - AndroidLeonardo SibelaView Answer on Stackoverflow
Solution 8 - AndroidRasoul MiriView Answer on Stackoverflow
Solution 9 - AndroidGanesh HegdeView Answer on Stackoverflow
Solution 10 - AndroidmistView Answer on Stackoverflow