'Android TextView padding between lines
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?
Solution 1:[1]
If you want padding between text, try LineSpacingExtra="10sp"
<TextView
android:layout_width="match_parent"
android:layout_height="180dp"
android:lineSpacingExtra="10sp"/>
Solution 2:[2]
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)
Solution 3:[3]
This supplemental answer shows the effect of changing the line spacing.
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 4:[4]
Adding android:lineSpacingMultiplier="0.8" can make the line spacing to 80%.
Solution 5:[5]
You can use TextView.setLineSpacing(n,m) function.
Solution 6:[6]
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 7:[7]
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 8:[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.
Solution 9:[9]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow

