10

I am trying to customize materials TextInpuLayout.OutlinedBox and TextInputEditText.

My current state is like following image

enter image description here

What I want to do is remove the background of the hint label so that it doesn't create that ugly cutout. Like this:

enter image description here

Or if that isn't possible moving the label above the input would also do the trick:

enter image description here

It would be nice if this was achievable using styles so that I could easily apply this to other text input elements as well. I'm pretty new to android so please be considerate.

Here's the code for the styling:

<style name="MyTheme" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
    <item name="android:textSize">14sp</item>
    <item name="boxCornerRadiusTopStart">@dimen/textInputCornerRadius</item>
    <item name="boxCornerRadiusTopEnd">@dimen/textInputCornerRadius</item>
    <item name="boxCornerRadiusBottomStart">@dimen/textInputCornerRadius</item>
    <item name="boxCornerRadiusBottomEnd">@dimen/textInputCornerRadius</item>
    <item name="boxBackgroundColor">@color/primaryDarkColorBackground</item>
    <item name="boxStrokeColor">@color/text_input_box_stroke</item>
</style>
<style name="EditTextStyle" parent="Widget.AppCompat.EditText">
    <item name="android:background">@null</item>
    <item name="android:paddingStart">30dp</item>
</style>

And here is how I define my input in the layout:

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/username_layout"
        style="@style/MyTheme"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="48dp"
        android:layout_marginEnd="48dp"
        android:hint="@string/email_or_username"
        app:boxBackgroundMode="outline"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.10">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/username"
            style="@style/EditTextStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </com.google.android.material.textfield.TextInputLayout>
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
ThizzHead
  • 101
  • 1
  • 6

2 Answers2

1

I don't think you can obtain it with the TextInputLayout.OutlinedBox style.
It is not exactly what you are looking for:

   <com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.Rounded"
        ..>

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingStart="30dp"
            />

    </com.google.android.material.textfield.TextInputLayout>

With:

  <style name="Widget.MaterialComponents.TextInputLayout.FilledBox.Rounded" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
    <item name="shapeAppearanceOverlay">@style/Rounded_ShapeAppearanceOverlay.MaterialComponents.TextInputLayout</item>
    <item name="boxStrokeColor">@color/input_text_no_underline</item>
  </style>

  <style name="Rounded_ShapeAppearanceOverlay.MaterialComponents.TextInputLayout" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">50%</item>
  </style>

The selector is used to remove the underline:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:alpha="0" android:color="?attr/colorOnSurface" android:state_hovered="true"/>
  <item android:alpha="0" android:color="?attr/colorOnSurface" android:state_enabled="false"/>
  <item android:alpha="0" android:color="?attr/colorOnSurface"/>
</selector>

enter image description here enter image description here

Note: it requires the version 1.1.0 of the library.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • Hi Gabriele, this answer is very useful to me that I'm facing the same problem. I followed your solution but the underline remains. I have put the selector in the color.xml file like this: ..... Maybe the problem is ?attr/colorOnSurface? I don't have the attr directory. Thanks again for your help – michelecasati Mar 04 '20 at 09:43
  • @michelecasati Try to use the same color used in the background – Gabriele Mariotti Mar 04 '20 at 09:52
  • 2
    I have understood the problem. I am using a gradient as a background, so the color won't match in both cases. I have to figure it out another solution, thanks anyway for your quick help – michelecasati Mar 04 '20 at 10:15
-1

Try setting on the TextInputEditText:

android:background="@android:color/transparent"
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108