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!

rgv
  • 61
  • 10

2 Answers2

2

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
  • 21,986
  • 13
  • 70
  • 74
  • @RaghavAmbashta: Have you tried it? – aminography Aug 04 '20 at 19:17
  • I tried but It showed an error. – rgv Aug 04 '20 at 19:38
  • What does the error say? – aminography Aug 04 '20 at 19:47
  • The app just gets crashed every time – rgv Aug 04 '20 at 20:13
  • Ok, please post the logcat content to see what's wrong. – aminography Aug 05 '20 at 03:12
  • 2020-08-05 13:48:42.183 7727-7727/com.example.test E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.test, PID: 7727 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class com.google.android.material.textfield.TextInputLayout at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) – rgv Aug 05 '20 at 08:21
  • I got the solution. I had to update theme in my @style to Theme.MaterialComponents.NoActionBar Thank You – rgv Aug 05 '20 at 08:42
  • Good, You're welcome, I'm happy to see the problem is resolved :) – aminography Aug 05 '20 at 10:38
  • sure..I ll do it – rgv Aug 05 '20 at 13:35
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
  • 8
  • What is @+id/tvHeader ? – rgv 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 – rgv 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