Android: TextView: Remove spacing and padding on top and bottom
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"
Solution 5
Updated XML
android:fontFamily="monospace"
android:includeFontPadding="false"
Related videos on Youtube
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, 2022Comments
-
700 Software about 2 years
When I have a
TextView
with a\n
in the text,, on the right I have twosingleLine
TextView
s, one below the other with no spacing in between. I have set the following for all threeTextView
s.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 rightTextView
.The second line of the left
TextView
is a little higher than the second line of the bottom rightTextView
.It seems that there is some kind of hidden padding on the top and the bottom of the
TextView
s. How can I remove that?-
Dawid Hyży over 10 yearstry to set gravity of this textview to center_vertical
-
mmm2006 over 9 yearsHi,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 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 about 7 yearsThis didn't work for me
-
Richa over 4 yearsany of below answers didnt work for me, can you help
-
-
700 Software over 13 yearsBut 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 over 11 yearsdo 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 about 10 yearsThis can lead to the bottom of the text potentially getting cut off
-
Autobots almost 10 yearsBut it seems always has a
1dp
top/bottom padding, am i wrong?(I useDeveloper options -> Show layout bounds
) -
theblang almost 10 years
includeFontPadding="false"
does remove some of the space, but not all of it. very strange. -
Elliott over 9 yearsThis 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 over 9 yearsandroid:layout_marginTop="-5dp" android:layout_marginBottom="-5dp" (Optional) has worked for me.
-
CoDe about 9 yearsAfter setting runtime it's not taking top padding but bottom padding still there, even left and right padding as well? any suggestion?
-
vovahost over 8 yearsPay 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 about 8 yearsFor 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 about 8 yearsReally bad idea of use negative margin. Better to consider a custom view and draw the text exactly as you want.
-
Bawa over 7 yearsperfect for my situation, way better than removing font padding. +1
-
Tushar about 7 yearsIs this question or answer?
-
Linsh about 7 years@Tushar It's an answer.
-
Tushar about 7 yearsOk. Can you please add some explanation with code here.
-
Linsh about 7 years@Tushar Sure, but it's a little more.
-
Makyen about 7 yearsWhile 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 about 7 yearsSubclassing from
TextView
and especially overridingonDraw
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 theAppCompat
as well) and you can't match OEM-specific style of this control on particular device if there is one. -
Marty Miller about 7 yearsThis 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 about 7 yearsadding these two line is not working for me and also there is no change in padding
-
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 about 7 years@sunilkushwah You can probably push it on github and share the link
-
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 about 7 yearsi tried this but its not working,Note :i don't want any default padding in TextView
-
Mohammed Atif about 7 yearsWhat 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 about 7 yearsmeans when i view my layout in auto layout bounds it will not show the white space around Textview.
-
Mohammed Atif about 7 yearsCan you upload the picture of screenshot and send the link please
-
sunil kushwah about 7 yearsLet us continue this discussion in chat.
-
Linsh about 7 yearsI changed in a new way.
-
hman about 6 yearsincludeFontPadding="false" was exactly what I needed! Thanks.
-
phnghue over 5 yearsIf patch like this, you should calculate the ratio of font size too.
-
Nico Haase over 5 yearsPlease add some explanation about that
-
Anshul Rawat over 5 yearsi thought this must be understood. I can only be used in xml .
-
Adil Soomro about 4 yearsThis trick may not work for different font, sizes or layouts.
-
Adil Soomro about 4 yearsIt 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 about 4 yearsHi, 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 about 4 yearsActually 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 about 4 years@DarekDeoniziak Some fonts have spaces
-
Reza over 3 yearsTnx. 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 almost 3 yearssame, not working. still extra padding on the top
-
Jack almost 3 years@MartyMiller add a attribute on your textview, android:letterSpacing="0"