277

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?

Johnny Five
  • 987
  • 1
  • 14
  • 29
Cagatay Gurturk
  • 7,186
  • 3
  • 34
  • 44

11 Answers11

485

You can use lineSpacingExtra and lineSpacingMultiplier in your XML file.

Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
Romain Guy
  • 97,993
  • 18
  • 219
  • 200
  • 18
    lineSpacingMultiplier works for me with float values like: android:lineSpacingMultiplier="0.8" – Juan Saravia Mar 25 '15 at 16:58
  • 9
    Can you explain how these work? Where are these measurements taken from? Can you give examples with relation to the font and also language? For example, line spacing is measured from the baseline, but East Asian language have no baseline. Where is the default line spacing defined? Is it in the font itself? – Christopher Perry Aug 01 '16 at 18:52
  • 4
    For example: android:lineSpacingExtra="2dp" – nibbana Apr 16 '20 at 13:59
  • 1
    When I set text background, the background also covers the paddings. – user1034912 May 15 '21 at 03:55
104

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

<TextView
    android:layout_width="match_parent"
    android:layout_height="180dp"
    android:lineSpacingExtra="10sp"/>
Ola Ström
  • 4,136
  • 5
  • 22
  • 41
chanu panwar
  • 1,076
  • 1
  • 7
  • 4
87

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

sealz
  • 5,348
  • 5
  • 40
  • 70
61

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.

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
20

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

V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
Ajitsen
  • 338
  • 3
  • 7
18

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

Vishnu Haridas
  • 7,355
  • 3
  • 28
  • 43
9

You can either use lineSpacingExtra or 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="0.8" />

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

Leonardo Sibela
  • 1,613
  • 1
  • 18
  • 39
3

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"
Rasoul Miri
  • 11,234
  • 1
  • 68
  • 78
0

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.

0

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.

Antoine
  • 1,393
  • 4
  • 20
  • 26
mist
  • 1
0

It's working for me

just add two lines to your Textview

 android:includeFontPadding="false"
 android:lineSpacingMultiplier="0.8"
<TextView
            android:id="@+id/searchByItemTv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:fontFamily="@font/dm_sans"
            android:includeFontPadding="false"
            android:lineSpacingMultiplier="0.8"
            android:paddingVertical="3dp"
            android:paddingStart="4dp"
            android:paddingEnd="6dp"
            android:text="@string/date_of_birth"
            android:textColor="@color/text_black"
            android:textSize="16dp" />

note: don't use android:textAppearance=""

before :enter image description here

after: enter image description here

Maruf Alam
  • 228
  • 4
  • 10