Ziad H.
Ziad H.

Reputation: 699

how to resize the textField and disable floating label in a TextInputLayout?

I'm using the Material design library to make a rounded editText, and I was able to do that but now I want it to look a bit smaller, I used the dense textField style but I still want the height of the view to be smaller than that. The problem is that floating Label tag, I haven't set a hint for the textField but the Label Tag is still taking up some empty space.

<com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.Dense"
        android:id="@+id/editText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:endIconMode="custom"
        app:endIconDrawable="@drawable/ic_speaker_24dp"
        app:layout_constraintBottom_toBottomOf="@+id/topBar_view"
        app:layout_constraintEnd_toEndOf="@+id/topBar_view"
        app:layout_constraintStart_toEndOf="@+id/separater_line"
        app:layout_constraintTop_toTopOf="parent"
        app:boxCornerRadiusTopStart="20dp"
        app:boxCornerRadiusTopEnd="20dp"
        app:boxCornerRadiusBottomStart="20dp"
        app:boxCornerRadiusBottomEnd="20dp">

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

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

I just want the textField to look like the chrome url search bar, slim and the hint disappear when typing, not a floating Label.

Edit: I tried the app:hintEnabled="false" attribute but there is still an empty space

Upvotes: 6

Views: 7480

Answers (3)

nhcodes
nhcodes

Reputation: 1974

Just set the TextInputEditText padding to 8dp for example. You shouldn't set a label/hint in that case.

        <com.google.android.material.textfield.TextInputLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/input_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="8dp"
                android:text="Some text" />

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

Will look like this: padding set to 8dp

Upvotes: 2

Ankita Bhatt
Ankita Bhatt

Reputation: 21

<com.google.android.material.textfield.TextInputLayout
                android:id="@+id/etemailLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint=" ">

        <EditText
            android:id="@+id/txtemail"
            style="@style/login_edittext"
            android:hint="Enter Your Email Address"
            android:inputType="textEmailAddress" />

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

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/etPasswordLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint=" "
        android:scrollbars="vertical"
        app:counterEnabled="true"
        app:passwordToggleEnabled="true">

        <EditText
            android:id="@+id/txtpassword"
            style="@style/login_edittext"
            android:hint="Enter Password"
            android:inputType="textPassword"
            android:maxLength="8"
            android:scrollbars="vertical" />

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

for apply style to your EditText put below code in Styles.xml ..

<style name="login_edittext">
        <item name="android:layout_marginTop">15dp</item>
        <item name="android:background">@drawable/edittext_background</item>
        <item name="android:textColorHint">#FFFFFF</item>
        <item name="android:textColor">#FFFFFF</item>
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
    </style>

for backgroud effect create edittext_background.xml in drawable..

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners android:radius="7dp" />
    <solid android:color="#80ffffff" />
    <padding android:left="10dp" android:right="10dp"
        android:top="10dp" android:bottom="10dp" />
    <stroke android:width="2dp" android:color="#FFFFFF" />
</shape>

Upvotes: 0

Gabriele Mariotti
Gabriele Mariotti

Reputation: 364391

You can use app:hintEnabled="false" to disable the floating label functionality. Also you can customize the dense style.

You can use something like:

  <com.google.android.material.textfield.TextInputLayout
        app:hintEnabled="false"
        style="@style/MyDenseOutlined"
        ...>

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

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

with this style:

  <style name="MyDenseOutlined" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
    <item name="materialThemeOverlay">@style/MyThemeOverlayOutlinedDense</item>

  </style>

  <style name="MyThemeOverlayOutlinedDense">
    <item name="editTextStyle">@style/MyTextInputEditText_outlinedBox_dense_h
    </item>
  </style>

  <style name="MyTextInputEditText_outlinedBox_dense_h" parent="@style/Widget.MaterialComponents.TextInputEditText.OutlinedBox.Dense">
    <item name="android:paddingTop">8dp</item>
    <item name="android:paddingBottom">8dp</item>
  </style>

Here the results (with a default style and the custom style):

enter image description here enter image description here

Upvotes: 29

Related Questions