1

How to move the hint of EditText (Android Java) to the top of the view while typing? I have attached 2 images to show how I want to implement my EditText View.

enter image description here To enter image description here

How to make search country hint at the top of view as per the image?

Please help!

2 Answers2

1

Using TextInputLayout you can achieve this.

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/searchCountriesInputLayout"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/searchCountriesEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Search Countries" />

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

In build.gradle:

dependencies {
    implementation 'com.google.android.material:material:1.1.0'
}
aminography
  • 20,387
  • 13
  • 62
  • 71
1

You can user TextInputLayout for this :-

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/tilSearchCountry"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/padding_normal"
        android:hint="@string/your_hint"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

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

In case if you haven't included google material library add this in your build.gradle:

dependencies {
    implementation 'com.google.android.material:material:1.1.0'
}
Aditya Tyagi
  • 431
  • 3
  • 7
  • What is @+id/tvHeader ? – Raghav Ambashta Aug 04 '20 at 20:20
  • Sorry @RaghavAmbashta that was my fault i was trying to use this in one of my layout in order to provide you the solution and ended up with that tag i have updated the answer – Aditya Tyagi Aug 05 '20 at 05:03
  • Also need to update @style theme to Theme.MaterialComponents.NoActionBar and android:layout_margin="@dimen/padding_normal" shows error – Raghav Ambashta Aug 05 '20 at 08:44
  • padding_normal is just a standard padding that you might have defined in your dimens.xml if not just use 16dp instead or according to your requirement that's just standard naming conventions for padding in application don't get confused with it use padding according to your requirement. – Aditya Tyagi Aug 05 '20 at 09:04