Android EditText


Download using android EditText sample

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

Android EditText Usage


An EditText can be added to a layout with all default behaviors with the following XML:

How to retrieve & set value in a EditText?


Retrieving & setting value in a EditText is as follows:

Text decoration in android?


Android provides several text decorating attributes like Typeface, font family, text style, text size, text color, text shadow etc. which you can learn here:

How to decorate text in android

How to handle TextChangedListener?


On text change of EditText or editable TextView, if you want to perform some operation then you need to handle TextChangedListener.

Result

Text change listener

How to handle OnEditorActionListener?


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:

  1. actionDone
  2. actionGo
  3. actionNext
  4. actionPrevious
  5. actionSearch
  6. actionSend

Result

Editor action listener

Using android:lines, android:digits, android:maxLength?


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.

How to display image as part of EditText?


Here are the drawable attribute that can be use to display image as part of content:

  1. android:drawableStart : The drawable to be drawn to the start of the text.
  2. android:drawableEnd : The drawable to be drawn to the end of the text.
  3. android:drawableLeft : The drawable to be drawn to the left of the text.
  4. android:drawableRight : The drawable to be drawn to the right of the text.
  5. android:drawablePadding : The padding between the drawables and the text.

Result

Image in EditText

How to specify keyboard type?


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:

  1. text
  2. textPassword
  3. date
  4. datetime
  5. number
  6. numberDecimal
  7. numberPassword
  8. numberSigned
  9. phone
  10. textEmailAddress
  11. textUri
  12. textPersonName

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:

  • textCapSentences : Normal text keyboard that capitalizes the first letter for each new sentence.
  • textCapWords : Normal text keyboard that capitalizes every word. Good for titles or person names.
  • textAutoCorrect : Normal text keyboard that corrects commonly misspelled words.
  • textPassword : Normal text keyboard, but the characters entered turn into dots.
  • textMultiLine : Normal text keyboard that allow users to input long strings of text that include line breaks (carriage returns).

A typical example of using android:InputType is given below:

how to display placeholder Hints?


we can display hint for EditText by using android:hint. An example is as follows:

how to change bottomline color?


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

Example of android EditText

Result

Android EditText

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

activity_main.xml

Step 3: Open res -> values -> strings.xml and replace it with below given code.

strings.xml

Step 4: Open res -> values -> styles.xml and replace it with below given code.

styles.xml

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:

MainActivity.java

Step 7: Clean and run the project to see the result.


Share & Like

Tutorial topics

About ApnaTutorials.com

ApnaTutorials.com is for Java, J2ee, Html5 and android developers. All tutorials are well explained and are easy to understand. We have tested every tutorials on our developement environment before posting it in public domain.