Skip to content

Working with the EditText

Nathan Esquenazi edited this page Apr 4, 2014 · 31 revisions

Overview

The EditText is the standard text entry widget in Android apps. If the user needs to enter text into an app, this is the primary way for them to do that.

EditText

There are many important properties that can be set to customize the behavior of an EditText. Several of these are listed below. Check out the official text fields guide for even more input field details.

Usage

An EditText is added to a layout with all default behaviors with the following XML:

<EditText
    android:id="@+id/et_simple"
    android:layout_height="wrap_content"
    android:layout_width="match_parent">
</EditText>

Note that an EditText is simply a thin extension of the TextView and inherits all of the same properties.

Retrieving the Value

Getting the value of the text entered into an EditText is as follows:

EditText simpleEditText = (EditText) findViewById(R.id.et_simple);
String strValue = simpleEditText.getText().toString();

Customizing the Input Type

By default, any text contents within an EditText control is displayed as plain text. By setting inputType, we can facilitate input of different types of information, like phone numbers and passwords:

<EditText
    ...
    android:inputType="phone">
</EditText>

Most common input types include: textMultiLine, textUri, textEmailAddress, textPersonName, textPassword, number, phone, date, time. You can see a list of all available input types here.

Further Entry Customization

You might want to use the capitalize attribute to automatically capitalize each sentence, word or character:

<EditText
    ...
    android:capitalize="sentences">
</EditText>

Similarly, you can limit the characters that can be entered into a field using the digits attribute:

<EditText
    ...
    android:inputType="number"
    android:digits="01">
</EditText>

This would restrict the digits entered to just "0" and "1". We might want to limit the total number of characters with:

<EditText
    ...
    android:maxLength="5">
</EditText>

We might also want to limit the entry to a single-line of text (avoid newlines):

<EditText
    ...
    android:inputType="text"
    android:maxLength="5">
</EditText>

Displaying Placeholder Hints

You may want to set the hint for the EditText control to prompt a user for specific input with:

<EditText
    ...
    android:hint="@string/my_hint">
</EditText>

which results in:

Hints

Listening for EditText Input

Check out the basic event listeners cliffnotes for a look at how to listen for changes to an EditText and perform an action when those changes occur.

Providing Auto-complete

Check out the official text fields guide for a step-by-step on how to setup autocomplete for the entry.

References

Finding these guides helpful?

We need help from the broader community to improve these guides, add new topics and keep the topics up-to-date. See our contribution guidelines here and our topic issues list for great ways to help out.

Check these same guides through our standalone viewer for a better browsing experience and an improved search. Follow us on twitter @codepath for access to more useful Android development resources.

Clone this wiki locally