A EditText allows us to type text into an app. It can be either single line or multi-line. Touching a text field places the cursor and automatically displays the keyboard. In addition to typing, text fields allow for a variety of other activities, such as text selection (cut, copy, paste).
An EditText can be added to a layout with all default behaviors with the following XML:
Retrieving & setting value in a EditText is as follows:
Android provides several text decorating attributes like Typeface, font family, text style, text size, text color, text shadow etc. which you can learn here:
On text change of EditText or editable TextView, if you want to perform some operation then you need to handle TextChangedListener.
If you want to perform some action when user click on a action button then first you need to configure action button as follows:
now, add Editor listener for etImeOptiondemo in our activity.
Possible ImeOptions are as follows:
If we want to avoid new line entries in EditText, then we will use EditText as follows:
We can limit characters that can be enter in a EditText. For example, in below given EditText user will not be able to enter anything except 0 and 1.
android:maxLength specify maximum number of characters that can be entered in a EditText.
Here are the drawable attribute that can be use to display image as part of content:
You can specify the type of keyboard you want for your EditText object with the android:inputType attribute. Some most common values of android:inputType are as follows:
The android:inputType also allows you to specify certain keyboard behaviors, such as whether to capitalize all new words or use features like auto-complete and spelling suggestions.
The android:inputType attribute allows bitwise combinations so you can specify both a keyboard layout and one or more behaviors at once. Here are some of the common input type values that define keyboard behaviors:
A typical example of using android:InputType is given below:
we can display hint for EditText by using android:hint. An example is as follows:
If you are using AppCompat support library, then you can override the styles colorControlNormal, colorControlActivated, and colorControlHighlight to change bottomline color as follows:
Normal color is color which will displays when focus is not on the EditText
Activated color is the color which will be displays when focus is on the EditText
Highlight color is the color of selected text
Step 1: Create a new project in Android Studio and name it Android EditText.
Step 2: Open res -> layout -> activity_main.xml and add following code
Step 3: Open res -> values -> strings.xml and replace it with below given code.
Step 4: Open res -> values -> styles.xml and replace it with below given code.
Step 5: Either download icons that are used in the application from google or replace its name with some system icons.
Step 6: Open your MainActivity.java and replace it's code with below given code of f my MainActivity.java as follows:
Step 7: Clean and run the project to see the result.