EditText component

An EditText ui element allows to enter and modify text. There are also subclasses:

  • AutoCompleteTextView - an editable text view that shows completion suggestions automatically while the user is typing.
  • ExtractEditText - specialization of EditText for showing and interacting with the extracted text in a full-screen input method.
  • MultiAutoCompleteTextView - an editable text view, extending AutoCompleteTextView, that can show completion suggestions for the substring of the text where the user is typing instead of necessarily for the entire thing.

xml attributes

attribute description
inputType Helps an input method decide how to let the user enter text. Some possible values:
  • none - no content type, the text is not editable.
  • number
  • numberDecimal
  • text
  • textMultiLine
  • textPassword
  • textWebEmailAddress
  • textAutoCorrect - enables auto spelling correction, you can combine with other values with | character.
maxLines Specifies how many lines the EditText may have.
imeOptions Specifies additional features that you can enable in an IME associated with an editor, for example special action like
  • actionDone - allows to perform a "done" operation
  • actionSearch - allows to perform a "search" operation
  • actionSend - allows to perform a "send" operation
  • actionGo - allows to perform a "go" operation

For example, you want a single line EditText and show "Search" button on soft keyboard when editor is active:

<EditText
    android:id="@+id/edSearch"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:maxLines="1"
    android:imeOptions="actionSearch"
    />

TextWatcher interface

The TextWatcher interface allows you to handle a text changing. Listener will be called even text is changed within afterTextChanged. So we use inProcess to avoid loop. Also you can remove listener and set it again.

TextWatcher usage
val textWatcher = object: TextWatcher{
   
    var inProcess = false
    var prevString : CharSequence?

    // [start; start+count) characters in s are about 
    // to be replaced to [start; start+after)
    override fun beforeTextChanged(s: CharSequence?, start: Int, 
                 count: Int, after: Int) {
       if(!inProcess){ prevString = s }
    }

    override fun onTextChanged(s: CharSequence?, start: Int, 
                 before: Int, count: Int) {
        TODO("Not yet implemented")
    }

    override fun afterTextChanged(s: Editable?) {
        if(!inProcess){
        
            inProcess=true  // myEditText.removeTextChangedListener(this)
            
            // change s if necessary
            // ... do something
            
            inProcess=false // myEditText.addTextChangedListener(this)
        }
    }
}

myEditText.addTextChangedListener(textWatcher)

There is a Kotlin extension function doOnTextChanged to create a text watcher that only overrides the afterTextChanged method.

myEditText.doOnTextChanged { text, start, count, after -> 
        // action to be called after text change
}

show/hide soft keyboard

You can show and hide soft keyboard programmatically.

Code example
fun showSoftKeyboard() {
    myEditView.requestFocus()
    val imm: InputMethodManager = 
        getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
    imm.showSoftInput(myEditView, InputMethodManager.SHOW_IMPLICIT)
}

fun hideSoftKeyboard() {
    anyOtherView.requestFocus()
    val imm: InputMethodManager = 
        getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
    imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
}
void showSoftKeyboard() {
    myEditView.requestFocus();
    InputMethodManager imm = 
        (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
    imm.showSoftInput(myEditView, InputMethodManager.SHOW_IMPLICIT);
}

void hideSoftKeyboard() {
    anyOtherView.requestFocus();
    InputMethodManager imm = 
        (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}

IME actions

Most soft input methods provide a user action button in the bottom corner that's appropriate for the current text field. By default, the system uses this button for either a Next or Done action unless your text field allows multi-line text (such as with android:inputType="textMultiLine"), in which case the action button is a carriage return. However, you can specify additional actions in android:imeOptions that might be more appropriate for your text field, such as Send or Go.

The OnEditorActionListener interface allows you to handle IME actions.

Code example
val editorActionListener = object: OnEditorActionListener {
    override fun onEditorAction(v: TextView, 
            actionId: Int, event: KeyEvent?): Boolean {
        when (actionId) {
            EditorInfo.IME_ACTION_SEARCH -> {
                hideSoftKeyboard()
                // todo
                return true
            }

            else -> {
                return false
            }
        }
   }
}

myEditText.setOnEditorActionListener(editorActionListener)

auto-complete suggestions

To provide suggestions to users as they type, you can use a AutoCompleteTextView subclass or AppCompatAutoCompleteTextView in androidx library.

val countries: Array<out String> = 
            resources.getStringArray(R.array.countries_array)
// Create the adapter and set it to the AutoCompleteTextView
ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries)
    .also { adapter ->
        autoEditText.setAdapter(adapter)
    }
String[] countries = 
            getResources().getStringArray(R.array.countries_array);
// Create the adapter and set it to the AutoCompleteTextView
ArrayAdapter<String> adapter =
        new ArrayAdapter<String>(this, 
                android.R.layout.simple_list_item_1, countries);
autoEditText.setAdapter(adapter);