Home » Android » Android TextView padding between lines

Android TextView padding between lines

Posted by: admin March 10, 2020 Leave a comment

Questions:

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?

How to&Answers:

You can use lineSpacingExtra and lineSpacingMultiplier in your XML file.

Answer:

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

Answer:

If you want padding between text try LineSpacingExtra="10dp"

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

Answer:

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.

Answer:

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

Answer:

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

Answer:

The solution is simple. Just use these two attributes in your TextView, lineSpacingExtra and lineSpacingMultiplier

For example,

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:lineSpacingMultiplier="1.5"
    android:lineSpacingExtra="5dp"/>