Reputation: 19552
I am using an EditText
and it always adds a bit of padding in my text to both left and right.
Adding android:includeFontPadding="false"
did not help and using negative android:layout_marginLeft
or android:layout_marginRight
just makes the EditText
"expand".
How can I strip all padding from the EditText
that is being added by default?
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="20dp"
android:fontFamily="roboto-regular"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:layout_marginLeft="-5dp"
android:layout_marginRight="-5dp"
android:includeFontPadding="false"
android:textSize="@dimen/size"
android:textColor="@color/color"
android:inputType="textCapWords"
android:hint="@string/hint"
android:editable="false"
/>
Upvotes: 15
Views: 20890
Reputation: 155
Indeed EditText has its own drawable padding which remains even after setting all paddings/margins to zero in xml. To fully remove padding from EditText you have to remove it from its drawable which in fact is InsetDrawable.
You can cast your drawable to InsetDrawable
, get the original drawable via getDrawable()
method. That original drawable doesn't have additional spacing at all and you can set it as a new background via setBackground()
to your EditText
widget.
val etRemovedSpacingAllSides = findViewById<EditText>(R.id.etRemovedSpacingAllSides)
if (etRemovedSpacingAllSides.background is InsetDrawable) {
val insetDrawable = etRemovedSpacingAllSides.background as InsetDrawable
val originalDrawable = insetDrawable.drawable!!
etRemovedSpacingAllSides.background = originalDrawable
}
But you'd rather want it to save its top and bottom insets, because otherwise EditText
looks ugly.
To preserve horizontal spacing you have to subtract original padding dimensions from inset drawable padding dimensions. I assume that padding is calculated by summing original drawable padding and inset drawable insets. So to retrieve only insets you have to perform subtraction.
Here is full example (Corresponds to "removed spacing only horizontal" EditText
on picture):
val editText = findViewById<EditText>(R.id.etRemovedSpacing)
if (editText.background is InsetDrawable) {
val insetDrawable = editText.background as InsetDrawable
val originalDrawable = insetDrawable.drawable!!
val insetDrawablePadding = Rect()
insetDrawable.getPadding(insetDrawablePadding)
val originalDrawablePadding = Rect()
originalDrawable.getPadding(originalDrawablePadding)
// We subtract original padding dimensions from inset drawable padding dimensions.
// We assume that padding is calculated by summing original drawable padding
// and inset drawable insets. So to retrieve only insets we have to perform subtraction
val insetDrawableInsets = Rect(
insetDrawablePadding.left - originalDrawablePadding.left,
insetDrawablePadding.top - originalDrawablePadding.top,
insetDrawablePadding.right - originalDrawablePadding.right,
insetDrawablePadding.bottom - originalDrawablePadding.bottom)
// Remove side spacing from editText background to make it fit fully into layout width
editText.background = InsetDrawable(
originalDrawable,
0, insetDrawablePadding.top, 0, insetDrawableInsets.bottom)
}
Upvotes: 1
Reputation: 25836
If you also want to remove underline padding add negative margin:
<EditText
...
android:layout_marginLeft="-4dp"
android:layout_marginRight="-4dp"
... />
Upvotes: 12
Reputation: 2611
To remove the padding on the left and right of the EditText
, you can use the following:
<EditText
...
android:paddingLeft="0dp"
android:paddingRight="0dp"
... />
Upvotes: 47