24

I was wondering if there was any way that I could get a hint at the bottom of an Edit Text view -- and then the user to start entering text at the top of the box.

As a bonus question, is there any way I can make the hint NOT disappear once the user starts entering text.

hwrdprkns
  • 7,525
  • 12
  • 48
  • 69

3 Answers3

36

You can set the position of the text using the "gravity" attribute (as noted at http://developer.android.com/reference/android/widget/TextView.html#setGravity(int)). So to put the text at the bottom you would have;

android:gravity="bottom"

And to answer your bonus question; No, you can't display the hint when text is entered into the edit text view. Displaying the hint only when the box is empty is the defined behaviour as noted at http://developer.android.com/reference/android/widget/TextView.html#setHint(int)

(and yes, I know the links are for TextView, but EditText derives all of its' text positioning and hint handling functionality from TextView).

Al Sutton
  • 3,904
  • 1
  • 22
  • 17
  • 1
    Thanks, I figured this was the case. I was trying to have a live text counter display as a hint at the bottom of the textbox using the TextWatcher functionality. I've gotten the live text counter and the android:gravity stuff to work -- however I think that putting the hint inside of the EditText field would be cool... Too bad it can't be done. – hwrdprkns Jul 05 '10 at 20:13
8

Actually THERE IS a way to prevent hint from hiding, and it's a cool one :-)

It gives you the Floating Label look with smooth animation very easily and it's from android itself. No extra libraries and stuff.

Try this:

<android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Touch me and I'll fly!"/>
</android.support.design.widget.TextInputLayout>

You can change the behaviour using other xml tags like android:gravity="start|center|end" and others.

As a BONUS, you can use error messages with it :-) Here's the link to that question: https://stackoverflow.com/a/30953551/6474744

And sorry I do not have enough reputatuion to post images, so help yourself:

http://i0.wp.com/androidlift.info/wp-content/uploads/2015/09/Screenshot_2015-09-28-17-03-561.png

Enjoy :-)

Pedram
  • 921
  • 1
  • 10
  • 17
  • 1
    Thanks, It's very cool, It's working and more important, it's opposed to accepted answer where it's says "And to answer your bonus question; No, you can't display the hint when text is entered into the edit text view. Displaying the hint only when the box is empty is the defined behaviour as noted at " – Saman Oct 21 '17 at 12:10
  • 1
    You will need to add design support library for TextInputLayout: implementation 'com.android.support:design:28.0.0' – Ashwin Feb 24 '19 at 01:45
4

The problem with using both android:gravity and android:hint is that they are inter-linked with regard to cursor position.When you position the hint using gravity and you start entering text, it is entered in the same position as the your hint which is a problem if you want it to start traditionally on the top-left corner.

surbian
  • 63
  • 4