EditText


public class EditText
extends TextView

java.lang.Object
   ↳ android.view.View
     ↳ android.widget.TextView
       ↳ android.widget.EditText


A user interface element for entering and modifying text. When you define an edit text widget, you must specify the R.styleable.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 R.styleable.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 TextView.addTextChangedListener method.

This widget does not support auto-sizing text.

XML attributes

See EditText Attributes, TextView Attributes, ERROR(View Attributes/android.R.styleable#View View Attributes)

Summary

XML attributes

android:enableTextStylingShortcuts Enables styling shortcuts, e.g. 

Inherited XML attributes

Inherited constants

Inherited fields

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 Selection.extendSelection.

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.

boolean isStyleShortcutEnabled()

Return true if style shortcut is enabled, otherwise returns false.

boolean onKeyShortcut(int keyCode, KeyEvent event)

Called on the focused view when a key shortcut event is not handled.

boolean onTextContextMenuItem(int id)

Called when a context menu option for the text view is selected.

void selectAll()

Convenience for Selection.selectAll.

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 index)

Convenience for Selection.setSelection(Spannable, int).

void setSelection(int start, int stop)

Convenience for Selection.setSelection(Spannable, int, int).

void setStyleShortcutsEnabled(boolean enabled)

Enables styls shortcuts, e.g. Ctrl+B for making text bold.

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

XML attributes

android:enableTextStylingShortcuts

Enables styling shortcuts, e.g. Ctrl+B for bold. This is off by default.

May be a boolean value, such as "true" or "false".

Public constructors

EditText

Added in API level 1
public EditText (Context context)

Parameters
context Context

EditText

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

Parameters
context Context

attrs AttributeSet

EditText

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

Parameters
context Context

attrs AttributeSet

defStyleAttr int

EditText

Added in API level 21
public 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
public void extendSelection (int index)

Convenience for Selection.extendSelection.

Parameters
index int

getAccessibilityClassName

Added in API level 23
public 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
public 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
public Editable getText ()

Return the text that TextView is displaying. If setText(java.lang.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.

isStyleShortcutEnabled

Added in API level 34
public boolean isStyleShortcutEnabled ()

Return true if style shortcut is enabled, otherwise returns false.

Returns
boolean true if style shortcut is enabled, otherwise returns false.

onKeyShortcut

Added in API level 1
public boolean onKeyShortcut (int keyCode, 
                KeyEvent event)

Called on the focused view when a key shortcut event is not handled. Override this method to implement local key shortcuts for the View. Key shortcuts can also be implemented by setting the shortcut property of menu items.

Parameters
keyCode int: The value in event.getKeyCode().

event KeyEvent: Description of the key event.

Returns
boolean If you handled the event, return true. If you want to allow the event to be handled by the next receiver, return false.

onTextContextMenuItem

Added in API level 3
public boolean onTextContextMenuItem (int id)

Called when a context menu option for the text view is selected. Currently this will be one of R.id.selectAll, R.id.cut, R.id.copy, R.id.paste, R.id.pasteAsPlainText (starting at API level\u202f23) or R.id.shareText.

Parameters
id int

Returns
boolean true if the context menu item action was performed.

selectAll

Added in API level 1
public void selectAll ()

Convenience for Selection.selectAll.

setEllipsize

Added in API level 1
public 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 TextUtils.TruncateAt.MARQUEE.

setSelection

Added in API level 1
public void setSelection (int index)

Convenience for Selection.setSelection(Spannable, int).

Parameters
index int

setSelection

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

Convenience for Selection.setSelection(Spannable, int, int).

Parameters
start int

stop int

setStyleShortcutsEnabled

Added in API level 34
public void setStyleShortcutsEnabled (boolean enabled)

Enables styls shortcuts, e.g. Ctrl+B for making text bold.

Parameters
enabled boolean: true for enabled, false for disabled.

setText

Added in API level 1
public 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. Subclasses overriding this method should ensure that the following post condition holds, in order to guarantee the safety of the view's measurement and layout operations: regardless of the input, after calling #setText both mText and mTransformed will be different from null.

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
protected 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
protected MovementMethod getDefaultMovementMethod ()

Subclasses override this to specify a default movement method.

Returns
MovementMethod