Android Edit Text Example

Android EditText Example

EditText is a Widget of user interface (UI) used to retrieve and modify text data from a user in an Android app. EditText is a subclass of TextView that inherit all the property of TextView. Nowadays, EditText is represented with the PlainText element in UI, which displays an empty text field while designing the app. EditText (or PlainText) is used in the app whenever you need input from the user side and proceed with its text (or value) in your app.

EditText (or PlainText) is not only used to get plain text in your application, but even you can use it to get values such as email, number, password, etc. To get the value of each type in EditText, you must specify its input type in its inputType attribute. For example, to input plain text, set the inputType attribute to "text", and to input only numeric values, set the inputType attribute to "number".

Syntax of EditText

Here the "Layout" could be any layout like "RelativeLayout", "LinearLayout", "GridLayout", etc., and the attributes can be any from the below-mentioned table:

XML Attributes of EditText

AttributesDescription
android:idIt is used to uniquely identify the element
android:widthIt is used to set the width of EditText in pixels
android:hightIt is used to set the height of EditText in pixels
android:textIt is used to set the text value inside the EditText
android:inputTypeIt is used to mention what type of value should pass in EditText, like plain text, email, password, etc
android:hintIt is used to highlight what should be input when text is empty
android:gravitygravity is used to align the text like left, right, center, top
android:textSizeIt is used to specify the size of the text
android:textStyleIt is used to set the text style like bold, italic, bold italic, etc.
android:textColorIt is used to set the color of the text
android:backgroundIt is used to set the background of your EditText
android:backgroundTintIt is used to set tint to the background of your element
android:maxWidthIt is used to set the maximum width of View in pixel
android:minWidthIt is used to set the minimum width of View in pixel
android:drawableStartIt is used to set the drawable to be drawn to the start of View
android:drawableEndIt is used to set the drawable to be drawn to the end of View
android:drawableBottomIt is used to set the drawable to be drawn to the bottom of View
android:drawableLeftIt is used to set the drawable to be drawn to the left of View
android:drawableRightIt is used to set the drawable to be drawn to the right of View
android:drawablePaddingIt is used to set the drawable to be drawn from the set padding of View

Example of using EditText

Look at this demonstration of how you can use the EditText widget in your user interface:

How you retrieve value from EditText in Java Class

Following is the code to retrieve the value from EditText in your Java class.

Android EditText Example

Now, let's create an example of Android EditText in which we will pass or input some values in the EditText user interface, get their value, and display their respective values in a Toast message.

Drag and drop two EditText widgets and one Button widget element on the "activity_main.xml" file (user interface) layout to do this. Look at the auto-generated XML code. If you wish to edit and modify, do accordingly.

activity_main.xml

Android EditText Example

MainActivity.java

In the "MainActivity.java" file, write a code to get values you will enter in EditText and display (manipulate) them in a Toast message on clicking the button.

Output:

Android EditText Example

How to use attributes of EditText

Let's see some of the common attributes of EditText that help to configure the user interface in an XML (layout) file.

1. id:

id attribute used to identify a text of EditText element uniquely. Following is the code to define and set the id attribute of EditText.

2. gravity:

gravity is an optional attribute used to align the input text right, left, center, center_vertical, center_horizontal, etc. Following is an example in which we have defined the gravity of text to the "right" in EditText.

3. text:

text attribute is used to set the text within an EditText control widget. You can set the text in both an XML file and the Java class. Following is an example in which we set the text value "Javatpoint" in EditText.

Setting text within EditText in the Java class

Here is the code to set text within EditText programmatically in the Java class.

4. hint:

hint attribute is used to set the hint, i.e., what the user has to enter in this edit text. Whenever the user starts typing (or providing) value in it, the hint text automatically goes disappears. Following is the code where we set the hint "enter your name" in the edit text.

Setting hint within EditText in Java class

Here is the code to set hint within EditText in Java class programmatically.

5. textColor:

textColor attribute is used to set the text color of the text of EditText. You can set the text color like "#rgb", "#rrggbb". Following is the code to set the text color in an XML file of EditText control; here, we set the red color.

Setting textColor within EditText in the Java class

Here is the code to programmatically set the text color within EditText in the Java class.

6. textColorHint:

textColorHint attribute is used to set the color of hint text in EditText. Following is the code to set the color of hint text in an XML file; here, we set the blue color of hint text.

Setting textColorHint within EditText in Java class

Following is the code to programmatically set the color of hint text within EditText in the Java class.

7. textSize:

textSize attribute is used to set the size of the text in edit text. You can set the size of edit text in a different unit like sp (scale independent pixel) or dp (density pixel). In the below example, we set 25sp size for the text of an edit text.

Setting textSize within EditText in Java class

Also, you can set the text size of EditText in Java class programmatically as follow.

8. textStyle:

textStyle attribute is used to set the style of text in EditText. The different text styles are bold, italic, and normal (default style). If you want to set two or more styles for an edit text, use the "|" operator. In the below example, we set the text style to bold and italic.

9. background:

background attribute is used to set the background of an EditText. You can set a color or a drawable image in the background of an edited text. Here, we set the black background of EditText.

Setting Background within EditText in Java class

Also, you can set the background color of EditText in the Java class programmatically as follow.

10. padding:

padding attribute is used to set the padding of text as left, right, top or bottom. In the above example (in 9 background), we set the 15dp padding from all the sides of the edit text.