Add EditText Programmatically – Android

Home / Android / Add EditText Programmatically – Android

Sometimes it is the requirement of the program to add EditText programmatically. The following code can be used to add the EditText component in Android application.

To create a new instance of EditText

EditText editText = new EditText(context);

It sets the property of EditText to single line. The keyboard <enter> key is replaced by Done, if there is no next EditText available. But, the presence of next EditText will replace the <enter> key to Next

editText.setSingleLine();

This line sets the label of ImeAction to Done and the button click will terminate the input.

editText.setImeActionLabel("Done", EditorInfo.IME_ACTION_DONE);

The presence of next EditText will require this option to replace the Next with Done

editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

This function makes the input type of EditText a password.

editText.setInputType(InputType.TYPE_CLASS_TEXT | 
         InputType.TYPE_TEXT_VARIATION_PASSWORD);

This function transforms the input value to a (dot) after some interval or after input of next character before the interval is completed.

editText.setTransformationMethod(PasswordTransformationMethod.getInstance());




The editText can be passed into the addView() of any View or its child.

Visit Android Developer website for more information about EditText.