7

I have an EditText defined like this:

    <EditText
        android:id="@+id/input_password"
        android:hint="@string/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword" />

And it has some default paddings, looking like this with "Show layout bounds" option: enter image description here (or is it really margins, I'm not sure. I'm referring to the space between red inner box and the blue corners). But what is the value of these paddings/margins? In the platforms/android-19/data/res/values/themes.xml I found Widget.EditText style defined like this:

<style name="Widget.EditText">
    <item name="android:focusable">true</item>
    <item name="android:focusableInTouchMode">true</item>
    <item name="android:clickable">true</item>
    <item name="android:background">?android:attr/editTextBackground</item>
    <item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item>
    <item name="android:textColor">?android:attr/editTextColor</item>
    <item name="android:gravity">center_vertical</item>
</style>

But there are no margins/paddings mentioned. So where are they defined?

netimen
  • 4,199
  • 6
  • 41
  • 65
  • it is margins...but maybe it´s not editText margins, maybe it´s the parent layout paddings... – Opiatefuchs Jul 03 '14 at 14:14
  • 1
    The answer to this question is given at http://stackoverflow.com/a/9384340/1307749 : EditText padding comes from its background, which is a 9-patch image with transparent padding. – asaveljevs Jun 06 '15 at 14:31

1 Answers1

2

This issue comes from the default value of ?android:attr/editTextBackground.

<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetBottom="@dimen/abc_edit_text_inset_bottom_material"
    android:insetLeft="@dimen/abc_edit_text_inset_horizontal_material"
    android:insetRight="@dimen/abc_edit_text_inset_horizontal_material"
    android:insetTop="@dimen/abc_edit_text_inset_top_material">

    <selector>
        <item
            android:drawable="@drawable/abc_textfield_default_mtrl_alpha"
            android:state_enabled="false" />
        <item
            android:drawable="@drawable/abc_textfield_default_mtrl_alpha"
            android:state_focused="false"
            android:state_pressed="false" />
        <item android:drawable="@drawable/abc_textfield_activated_mtrl_alpha" />
    </selector>

</inset>

As you can see the inset value for all edges of EditText is 4dp. That makes EditText has small padding around its content. To remove that padding you should create a new EditText style with modified editTextBackground attribute. For example:

<style name="Widget.App.TextField" parent="@style/Widget.AppCompat.EditText">
        <item name="colorControlActivated">#303E44</item>
        <item name="colorControlHighlight">#E5E9EC</item>
        <item name="colorControlNormal">#E5E9EC</item>
        <item name="editTextBackground">@drawable/background_new_edit_text</item>
<item name="android:editTextBackground">@drawable/background_new_edit_text</item>
    </style>

New background_new_edit_text.xml

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:inset="@dimen/spacing_0dp">
    <selector>
        <item
            android:drawable="@drawable/abc_textfield_default_mtrl_alpha"
            android:state_enabled="false" />
        <item
            android:drawable="@drawable/abc_textfield_default_mtrl_alpha"
            android:state_focused="false"
            android:state_pressed="false" />
        <item android:drawable="@drawable/abc_textfield_activated_mtrl_alpha" />
    </selector>

</inset>

Apply new style to your EditText:

<EditText
        android:id="@+id/edt_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/Widget.App.TextField"
        />
Raghul Vaikundam
  • 588
  • 4
  • 20
Hoa Nguyen
  • 91
  • 9