Android: TextView: Remove spacing and padding on top and bottom

194,559

Solution 1

setIncludeFontPadding (boolean includepad)

or in XML this would be:

android:includeFontPadding="false"

Set whether the TextView includes extra top and bottom padding to make room for accents that go above the normal ascent and descent. The default is true.

Solution 2

I searched a lot for proper answer but no where I could find an Answer which could exactly remove all the padding from the TextView, but finally after going through the official doc got a work around for Single Line Texts

android:includeFontPadding="false"
android:lineSpacingExtra="0dp"

Adding these two lines to TextView xml will do the work.
First attribute removes the padding reserved for accents and second attribute removes the spacing reserved to maintain proper space between two lines of text.

Make sure not to add lineSpacingExtra="0dp" in multiline TextView as it might make the appearance clumsy

Solution 3

I feel your pain. I've tried every answer above, including the setIncludeFontPadding to false, which did nothing for me.

My solution? layout_marginBottom="-3dp" on the TextView gives you a solution for the bottom, BAM!

Although, -3dp on layout_marginTop fails....ugh.

Solution 4

You can try to use this attribute(ConstraintLayout):layout_constraintBaseline_toBaselineOf

Like this:

app:layout_constraintBaseline_toBaselineOf="@+id/textView"

enter image description here

enter image description here

Solution 5

Updated XML

android:fontFamily="monospace"
android:includeFontPadding="false"
Share:
194,559

Related videos on Youtube

700 Software
Author by

700 Software

Join 700.social ! A happy medium between Facebook and Gab. :) The name is too long but the domain looks good. Also, Software Development / Consulting (423) 802-8971 700software.com old username: George Bailey (but now I use my real name) http://www.google.com/images?q=George+Bailey

Updated on April 05, 2022

Comments

  • 700 Software
    700 Software about 2 years

    When I have a TextView with a \n in the text,, on the right I have two singleLine TextViews, one below the other with no spacing in between. I have set the following for all three TextViews.

    android:lineSpacingMultiplier="1" 
    android:lineSpacingExtra="0pt" 
    android:paddingTop="0pt" 
    android:paddingBottom="0pt"
    

    The first line of the left TextView lines up perfectly with the top right TextView.

    The second line of the left TextView is a little higher than the second line of the bottom right TextView.

    It seems that there is some kind of hidden padding on the top and the bottom of the TextViews. How can I remove that?

    • Dawid Hyży
      Dawid Hyży over 10 years
      try to set gravity of this textview to center_vertical
    • mmm2006
      mmm2006 over 9 years
      Hi,George Bailey, Do you have solution after so long time? I encounter this problem too now. Can you give me your solution? thanks.
    • 700 Software
      700 Software over 9 years
      @mmm2006, Been so long I don't know what I ended up doing. Try the solutions in the answers. If that doesn't work, make a new question.
    • Marty Miller
      Marty Miller about 7 years
      This didn't work for me
    • Richa
      Richa over 4 years
      any of below answers didnt work for me, can you help
  • 700 Software
    700 Software over 13 years
    But then the top line would not line up. And actually I do not care about them lining up as much as I would really like to remove the spacing.
  • vault
    vault over 11 years
    do you have some free font to point me to? I need to have no margin on top without assigning negative margins! Thank you!
  • Jason Robinson
    Jason Robinson about 10 years
    This can lead to the bottom of the text potentially getting cut off
  • Autobots
    Autobots almost 10 years
    But it seems always has a 1dp top/bottom padding, am i wrong?(I use Developer options -> Show layout bounds)
  • theblang
    theblang almost 10 years
    includeFontPadding="false" does remove some of the space, but not all of it. very strange.
  • Elliott
    Elliott over 9 years
    This can be a double edged sword. includeFontPadding is great for removing any additional padding from the font itself but it can cause issues in languages that have ascenders and descenders. I would make sure if you do this that you test languages like spanish and thai if you support them.
  • Rohit Mandiwal
    Rohit Mandiwal over 9 years
    android:layout_marginTop="-5dp" android:layout_marginBottom="-5dp" (Optional) has worked for me.
  • CoDe
    CoDe about 9 years
    After setting runtime it's not taking top padding but bottom padding still there, even left and right padding as well? any suggestion?
  • vovahost
    vovahost over 8 years
    Pay attention because this will remove the padding needed to display some bottom parts of letters like "p", "y", g. These letters will be partially cut off.
  • Vennsoh
    Vennsoh about 8 years
    For normal alphabets A-Z, a-z, includeFontPadding = false should work nicely. @mattblang The extra tiny bit of padding is reserved for things like $ sign. So includeFontPadding should work nicely for a standard english keyboard. But for foreign languages like a Chinese character, you might get into cropping problem.
  • Flynn81
    Flynn81 about 8 years
    Really bad idea of use negative margin. Better to consider a custom view and draw the text exactly as you want.
  • Bawa
    Bawa over 7 years
    perfect for my situation, way better than removing font padding. +1
  • Tushar
    Tushar about 7 years
    Is this question or answer?
  • Linsh
    Linsh about 7 years
    @Tushar It's an answer.
  • Tushar
    Tushar about 7 years
    Ok. Can you please add some explanation with code here.
  • Linsh
    Linsh about 7 years
    @Tushar Sure, but it's a little more.
  • Makyen
    Makyen about 7 years
    While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post (and get up-votes). Remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply.
  • SergGr
    SergGr about 7 years
    Subclassing from TextView and especially overriding onDraw seems to me as a very bad idea. In such a way you will lose all the fancy decorations that Google does or will do in the future releases (and of course no features from the AppCompat as well) and you can't match OEM-specific style of this control on particular device if there is one.
  • Marty Miller
    Marty Miller about 7 years
    This didn't work for me. I don't understand what I'm missing. I still have unwanted space at the bottom of my TextView
  • sunil kushwah
    sunil kushwah about 7 years
    adding these two line is not working for me and also there is no change in padding
  • Mohammed Atif
    Mohammed Atif about 7 years
    @sunilkushwah, it will definitely work if you are doing it properly. Would be glad to help if you can provide more details about your problem.
  • Mohammed Atif
    Mohammed Atif about 7 years
    @sunilkushwah You can probably push it on github and share the link
  • Mohammed Atif
    Mohammed Atif about 7 years
    @sunilkushwah i see that you are using Brandon Font (Custom Font). Can you once try by removing fontPath attribute?
  • sunil kushwah
    sunil kushwah about 7 years
    i tried this but its not working,Note :i don't want any default padding in TextView
  • Mohammed Atif
    Mohammed Atif about 7 years
    What do you mean by any default padding? linespacing removes padding from bottom and fontpaddingexra removes the padding on top reserved for accents. What else do you want to remove?
  • sunil kushwah
    sunil kushwah about 7 years
    means when i view my layout in auto layout bounds it will not show the white space around Textview.
  • Mohammed Atif
    Mohammed Atif about 7 years
    Can you upload the picture of screenshot and send the link please
  • sunil kushwah
    sunil kushwah about 7 years
  • Linsh
    Linsh about 7 years
    I changed in a new way.
  • hman
    hman about 6 years
    includeFontPadding="false" was exactly what I needed! Thanks.
  • phnghue
    phnghue over 5 years
    If patch like this, you should calculate the ratio of font size too.
  • Nico Haase
    Nico Haase over 5 years
    Please add some explanation about that
  • Anshul Rawat
    Anshul Rawat over 5 years
    i thought this must be understood. I can only be used in xml .
  • Adil Soomro
    Adil Soomro about 4 years
    This trick may not work for different font, sizes or layouts.
  • Adil Soomro
    Adil Soomro about 4 years
    It may work but creating new TextView on every measure event is not memory efficient. Android Lint discourages creating new objects in draw(), onDraw() or any method that is related to layout and can be called unknown times during layout.
  • Federico Grandi
    Federico Grandi about 4 years
    Hi, welcome to Stack Overflow! When you answer a question you should include some kind of explanation, like what the author did wrong and what you did to fix it. I'm telling you this because your answer has been flagged as low-quality and is currently being reviewed. You can edit your answer by clicking the "Edit" button.
  • Darek Deoniziak
    Darek Deoniziak about 4 years
    Actually changing android:fontFamily is not necessary if you already have correct font. Basicly android:includeFontPadding is the only thing necessary and it has been mentioned in previous answers from the thread.
  • SJJ
    SJJ about 4 years
    @DarekDeoniziak Some fonts have spaces
  • Reza
    Reza over 3 years
    Tnx. this also helped me in an other problem which there is a textview that has multisizeed text. with this solution I can use multiple textview with there different textsizes and with the help of base line constraints I can show them like its one textview.
  • China fox
    China fox almost 3 years
    same, not working. still extra padding on the top
  • Jack
    Jack almost 3 years
    @MartyMiller add a attribute on your textview, android:letterSpacing="0"