Skip to content

Most visited

Recently visited

navigation

EditText

public class EditText
extends TextView

java.lang.Object
   ↳ android.view.View
     ↳ android.widget.TextView
       ↳ android.widget.EditText
Known Direct Subclasses
Known Indirect Subclasses


A user interface element for entering and modifying text. When you define an edit text widget, you must specify the TextView_inputType attribute. For example, for plain text input set inputType to "text":

 <EditText
     android:id="@+id/plain_text_input"
     android:layout_height="wrap_content"
     android:layout_width="match_parent"
     android:inputType="text"/>
Choosing the input type configures the keyboard type that is shown, acceptable characters, and appearance of the edit text. For example, if you want to accept a secret number, like a unique pin or serial number, you can set inputType to "numericPassword". An inputType of "numericPassword" results in an edit text that accepts numbers only, shows a numeric keyboard when focused, and masks the text that is entered for privacy.

See the Text Fields guide for examples of other TextView_inputType settings.

You also can receive callbacks as a user changes text by adding a TextWatcher to the edit text. This is useful when you want to add auto-save functionality as changes are made, or validate the format of user input, for example. You add a text watcher using the addTextChangedListener(TextWatcher) method.

This widget does not support auto-sizing text.

XML attributes

See EditText Attributes, TextView Attributes, View Attributes

Summary

Inherited XML attributes

From class android.widget.TextView
From class android.view.View

Inherited constants

From class android.widget.TextView
From class android.view.View

Inherited fields

From class android.view.View

Public constructors

EditText(Context context)
EditText(Context context, AttributeSet attrs)
EditText(Context context, AttributeSet attrs, int defStyleAttr)
EditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

Public methods

void extendSelection(int index)

Convenience for extendSelection(Spannable, int).

CharSequence getAccessibilityClassName()

Return the class name of this object to be used for accessibility purposes.

boolean getFreezesText()

Return whether this text view is including its entire text contents in frozen icicles.

Editable getText()

Return the text that TextView is displaying.

void selectAll()

Convenience for selectAll(Spannable).

void setEllipsize(TextUtils.TruncateAt ellipsis)

Causes words in the text that are longer than the view's width to be ellipsized instead of broken in the middle.

void setSelection(int start, int stop)

Convenience for setSelection(Spannable, int, int).

void setSelection(int index)

Convenience for setSelection(Spannable, int).

void setText(CharSequence text, TextView.BufferType type)

Sets the text to be displayed and the TextView.BufferType.

Protected methods

boolean getDefaultEditable()

Subclasses override this to specify that they have a KeyListener by default even if not specifically called for in the XML options.

MovementMethod getDefaultMovementMethod()

Subclasses override this to specify a default movement method.

Inherited methods

From class android.widget.TextView
From class android.view.View
From class java.lang.Object
From interface android.view.ViewTreeObserver.OnPreDrawListener
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource

Public constructors

EditText

added in API level 1
EditText (Context context)

Parameters
context Context

EditText

added in API level 1
EditText (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

EditText

added in API level 1
EditText (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

EditText

added in API level 21
EditText (Context context, 
                AttributeSet attrs, 
                int defStyleAttr, 
                int defStyleRes)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

defStyleRes int

Public methods

extendSelection

added in API level 1
void extendSelection (int index)

Convenience for extendSelection(Spannable, int).

Parameters
index int

getAccessibilityClassName

added in API level 23
CharSequence getAccessibilityClassName ()

Return the class name of this object to be used for accessibility purposes. Subclasses should only override this if they are implementing something that should be seen as a completely new class of view when used by accessibility, unrelated to the class it is deriving from. This is used to fill in AccessibilityNodeInfo.setClassName.

Returns
CharSequence

getFreezesText

added in API level 1
boolean getFreezesText ()

Return whether this text view is including its entire text contents in frozen icicles. For EditText it always returns true.

Returns
boolean Returns true if text is included, false if it isn't.

getText

added in API level 1
Editable getText ()

Return the text that TextView is displaying. If setText(CharSequence) was called with an argument of BufferType.SPANNABLE or BufferType.EDITABLE, you can cast the return value from this method to Spannable or Editable, respectively.

The content of the return value should not be modified. If you want a modifiable one, you should make your own copy first.

Returns
Editable The text displayed by the text view.

selectAll

added in API level 1
void selectAll ()

Convenience for selectAll(Spannable).

setEllipsize

added in API level 1
void setEllipsize (TextUtils.TruncateAt ellipsis)

Causes words in the text that are longer than the view's width to be ellipsized instead of broken in the middle. TextUtils.TruncateAt#MARQUEE is not supported.

Parameters
ellipsis TextUtils.TruncateAt: Type of ellipsis to be applied.

Throws
IllegalArgumentException When the value of ellipsis parameter is MARQUEE.

See also:

setSelection

added in API level 1
void setSelection (int start, 
                int stop)

Convenience for setSelection(Spannable, int, int).

Parameters
start int

stop int

setSelection

added in API level 1
void setSelection (int index)

Convenience for setSelection(Spannable, int).

Parameters
index int

setText

added in API level 1
void setText (CharSequence text, 
                TextView.BufferType type)

Sets the text to be displayed and the TextView.BufferType.

When required, TextView will use Spannable.Factory to create final or intermediate Spannables. Likewise it will use Editable.Factory to create final or intermediate Editables.

Parameters
text CharSequence: text to be displayed

type TextView.BufferType: a TextView.BufferType which defines whether the text is stored as a static text, styleable/spannable text, or editable text

Protected methods

getDefaultEditable

added in API level 1
boolean getDefaultEditable ()

Subclasses override this to specify that they have a KeyListener by default even if not specifically called for in the XML options.

Returns
boolean

getDefaultMovementMethod

added in API level 1
MovementMethod getDefaultMovementMethod ()

Subclasses override this to specify a default movement method.

Returns
MovementMethod

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)