When I have a TextView
with a \n
in the text,, on the right I have two singleLine
TextView
s, one below the other with no spacing in between. I have set the following for all three TextView
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 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 TextView
s. How can I remove that?
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 TextsAdding 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.
You can try to use this attribute(ConstraintLayout):
layout_constraintBaseline_toBaselineOf
Like this:
To my knowledge this is inherent to most widgets and the amount of "padding" differs among phone manufacturers. This padding is really white space between the image border and the image in the 9 patch image file.
For example on my Droid X, spinner widgets get extra white space than buttons, which makes it look awkward when you have a spinner inline with a button, yet on my wife's phone the same application doesn't have the same problem and looks great!
The only suggestion I would have is to create your own 9 patch files and use them in your application.
Ahhh the pains that are Android.
Edited: Clarify padding vs white space.
With an extra ImageView we can set the TextView to be baseline aligned with the ImageView and set the
android:baselineAlignBottom
on the ImageView to true, which will make the baseline of ImageView to bottom. Other views can align itself using the bottom of the ImageView which itself is the same as the TextView's baseline.This however only fixes the padding bottom not the top.
I remove the spacing in my custom view -- NoPaddingTextView.
https://github.com/SenhLinsh/NoPaddingTextView
Simply use