Android TextView padding between lines
AndroidTextviewAndroid 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)
Solution 4 - Android
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 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.