Android Developer Reference

This is the official Google Android site for developers. Here you will find all the documentation you need to develop rich android applications.

You will most likely visit this site often.

All tutorials and sample code are credited to their respected owners.

Portions of this page are reproduced from work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

EditText Widget

This topic covers the EditText widget and it's uses.

An EditText is a text based widget that is configured to be editable and will allow the user to edit the text inside. Tapping an EditText will display a cursor and the device software keyboard, allowing the user to enter new text or edit the existing text.

The EditText also has optional attributes such as size, font, and color that allow you to change the appearance of the text.

Here is a sample edit_text.xml file:

Notice here we are using the android:hint attribute instead of android:text. This displays the desired preset text but removes it as soon as the user starts typing a new value. You can access the text at runtime use the setText() and the getText() methods of the EditText widget.

To retrieve the value from an EditText you can do the following:

You can also listen for changes in the EditText, for instance; You can monitor when the text has changed, Keypresses, and checking certain characters for validation purposes.

Some of these methods and events are shown below.

Here is an example below that uses the setOnFocusChangedListener() to capture the text the user typed in.

As the user types into the EditText the event fires and retrieves the current value of the EditText control when it loses and gains focus.