In this tutorial, we will learn about android EditText. We're also going to go through various attributes used to customize it.
EditText can be defined as below –
"An Android EditText widget is user interface that are used to take input from user and modify the text."
While defining EditText widget, we can also specify android:inputType attribute. Based on the value given in this attribute, the keyboard type also displayed changes in acceptable characters and edit text appearance. For Example, If you set inputType to numberPassword.
Different attributes that are used to customise the EditText are listed below. However, you can check android official documentation for EditText to see the complete list of it’s attributes. Here, we are listing the commonly used attributes.
TextView and View inherit attributes in EditText. Some of the popular attributes are –
S. No. | XML Attributes | Description |
1 | android:id | This is unique id of the EditText to uniquely identify the EditText. |
2 | android:height | Sets height of the EditText. |
3 | android:width | Sets width of the EditText. |
4 | android:text | Sets the text of the EditText |
5 | android:textAllCaps | Use this attribute to show the text in capital letters. |
6 | android:textColor | Sets color of the text. |
7 | android:textSize | Sets size of the text. |
8 | android:textStyle | Sets style of the text. For example, bold, italic etc. |
9 | android:typeface | Sets typeface of the text. For example, normal, sans, serif, monospace. |
10 | android:hint | Hint to be shown when there is no text in the EditText. |
11 | android:inputType | This is used to define what are the types of data that can be entered by the user for this View. For example, Phone, Password, Number, Date, Time etc. Characters acceptable through keyboard will also change accordingly. |
12 | android:inputMethod | It sets, it specifies that edittext should use specified input method. |
13 | android:lines | If you want to set height of the View by number of lines, you can do it using this attribute. For example, android:lines=”2”, it means height of View will be 2 lines. |
14 | android:gravity | Sets gravity of the text. For example, center, horizontal_center, vertical_center etc. |
15 | android:elevation | Sets elevation to this view. |
16 | android:maxHeight | Sets maximum height of the View. |
17 | android:minHeight | Sets minimum height of the View. |
18 | android:maxLength | Sets maximum character length that can be entered in the View. |
19 | android:maxLines | Sets maximum lines this View can have. |
20 | android:minLines | Sets minimum lines this View can have. |
21 | android:maxWidth | Sets maximum width this View can have. |
22 | android:minWidth | Sets minimum width this View can have. |
23 | android:numeric | If sets, it specifies that EditText has numeric input method. |
24 | android:password | Use this attribute if you want to show the entered text as password dots. For example, If you enter ABCD, it will be shown as ****. |
25 | android:phoneNumber | If set, specifies that this EditText has a phone number input method. |
26 | android:background | Sets background to this View. |
27 | android:backgroundTint | Sets tint to the background. |
28 | android:clickable | Set true when you want to make this View clickable. Otherwise, set false. |
29 | android:drawableBottom | Sets drawable to bottom of the EditText. |
30 | android:drawableEnd | Sets drawable to end of the EditText. |
31 | android:drawableLeft | Sets drawable to left of the text. |
32 | android:drawablePadding | Sets padding to drawable. |
33 | android:drawableRight | Sets drawable to right of the EditText. |
34 | android:drawableStart | Sets drawable to start of the EditText. |
35 | android:drawableTop | Sets drawable to top of the EditText. |
In next, you will learn how to use android EditText widget in any android application. First we're going to create an android application, then we're going to use this widget.
To create a new android application, follow the steps below. If you have already created it, please ignore the steps.
S. No. | Steps |
1 | Open Android Studio. |
2 | Go to File => New => New Project. Write application name as EditText. Then, click next button. |
3 | Select minimum SDK you need. However, we have selected 14 as minimum SDK. Then, click next button. |
4 | Then, select Empty Activity => click next => click finish. |
5 | If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit tutorial to Create a New Project to know steps in detail. |
Since we have a new project now, to use EditText widget, we will modify xml and java. Please follow the steps below –
Open res/values/strings.xml file. Then, add below code into it.
<resources>
<string name="app_name">EditText</string>
<string name="hint_enter_something">Enter Something...</string>
</resources>
Open res/layout/activity_main.xml file. Add below code into it.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
android:ems="10"
android:hint="@string/hint_enter_something"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:inputType="text"/>
</android.support.constraint.ConstraintLayout>
We have added EditText in xml file.
Now, we will see MainActivity java file. Right now we don't make any changes. We just see it How our Java file look.
Open src/main/java/com.ukacademe.edittext/MainActivity.java file. Then, add below code into it
package com.ukacademe.edittext;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Since AndroidManifest.xml file is important file in any android application, we are also going to mention it here.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ukacademe.edittext">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Now run the application you will get output as shown below.