The horizontal space on the left and right of the EditText default drawable is controlled by the abc_edit_text_inset_horizontal_material
dimension. You can confirm that by looking at the abc_edit_text_material.xml
drawable file, which represents AppCompat's default EditText background. To remove the space completely you can just set the dimension to 0 by specifing the dimension with exact same name inside your own project's dimens.xml
file:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
<dimen tools:override="true" name="abc_edit_text_inset_horizontal_material">0dp</dimen>
...
</resources>
However, there is a catch. AppCompat library will only use it's own abc_edit_text_material.xml
background if the host OS doesn't support material design. So If you're running your app on Android 4, you'll see that side margins disappear after you add the dimension mentioned above. If, however, you launch your app on say Android 10, you'll see that margins are still there. That is because on newer Android versions, compat library will actually prefer background drawable specified inside the OS itself.
So you need to force all of your EditTexts to use abc_edit_text_material.xml
background specified inside the AppCompat library. Luckily, you can do that just by adding one line to your syles.xml
file:
styles.xml:
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="editTextBackground">@drawable/abc_edit_text_material</item>
...
</style>
...
</resources>
Any Theme.AppCompat.*
will do as a parent theme and of course you have to use this theme as your app's theme in order to get any effect.
This solution uses private AppCompat identifiers (Android Studio will complain about this), but I still think this solution is much cleaner than using negative margins.