Android EditText Widget Tutorial With Example

In this post, we will learn about android EditText widget. We will also go through different attributes that are used to customise it.

Output

Tutorialwing Android EditText widget Output Android Edittext tutorial

Tutorialwing Android EditText widget Output

Video Output

Getting Started

EditText widget 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 provided in this attribute, keyboard type shown also changes acceptable characters and appearance of the edit text. For Example, If you set inputType to numberPassword, the keyboard shown will be similar as below –

Tutorialwing Android EditText numberPassword input Type

Android EditText numberPassword input Type

Different attributes of Android EditText Widget

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.

Attributes in EditText are inherited from TextView and View. Some of the popular attributes are –

Sr. XML Attributes Description
1 android:background Sets background to this View.
2 android:backgroundTint Sets tint to the background.
3 android:clickable Set true when you want to make this View clickable. Otherwise, set false.
4 android:drawableBottom Sets drawable to bottom of the text.
5 android:drawableEnd Sets drawable to end of the text.
6 android:drawableLeft Sets drawable to left of the text.
7 android:drawablePadding Sets padding to drawable.
8 android:drawableRight Sets drawable to right of the text.
9 android:drawableStart Sets drawable to start of the text.
10 android:drawableTop Sets drawable to top of the text.
11 android:elevation Sets elevation to this view.
12 android:gravity Sets gravity of the text. For example, center, horizontal_center, vertical_center etc.
13 android:height Sets height of the EditText.
14 android:hint Hint to be shown when there is no text in the EditText.
15 android:inputMethod It sets, it specifies that edittext should use specified input method.
16 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.
17 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.
18 android:maxHeight Sets maximum height of the View.
19 android:minHeight Sets minimum height of the View.
20 android:maxLength Sets maximum character length that can be entered in the View.
21 android:maxLines Sets maximum lines this View can have.
22 android:minLines Sets minimum lines this View can have.
23 android:maxWidth Sets maximum width this View can have.
24 android:minWidth Sets minimum width this View can have.
25 android:numeric If sets, it specifies that EditText has numeric input method.
26 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 ****.
27 android:phoneNumber If set, specifies that this EditText has a phone number input method.
28 android:text Sets the text of the EditText
29 android:textAllCaps Use this attribute to show the text in capital letters.
30 android:textColor Sets color of the text.
31 android:textSize Sets size of the text.
32 android:textStyle Sets style of the text. For example, bold, italic, bolditalic etc.
33 android:typeface Sets typeface of the text. For example, normal, sans, serif, monospace.
34 android:width Sets width of the TextView.



Example Of Android EditText Widget

In this section, you will learn how to use android EditText widget in any android application. At First, we will create an android application, then, we will use this widget in application.

1. Creating New Project

Follow the steps below to create new android application. Please ignore the steps if you have already created it.

Step Description
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 17 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 post to create a new project to know steps in detail.

Since we have a new project now, we will modify xml and java to use EditText widget. Please follow the steps below –

2. Modify Values folder

Open res/values/strings.xml file. Then, add below code into it.

<resources>
	<string name="app_name">EditText</string>
	<string name="show_text">Show Text</string>
	<string name="hint_enter_something">Enter Something.......</string>
</resources>

Then, open res/values/dimens.xml file and add below code into it.

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<dimen name="padding">10dp</dimen>
	<dimen name="margin">10dp</dimen>
</resources>

Other values have not been changed. So, we are not going to mention it here.

3. Use EditText Widget in xml File

Open res/layout/activity_main.xml file. Add below code into it. You may also visit post to use EditText Widget programmatically in Android

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	android:id="@+id/activity_main"
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:gravity="center"
	android:orientation="vertical">

	<EditText
		android:id="@+id/editText"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:layout_margin="@dimen/margin"
		android:hint="@string/hint_enter_something"
		android:padding="@dimen/padding"
		android:inputType="numberPassword"/>

	<Button
		android:id="@+id/btnShow"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="@string/show_text">
	</Button>

</LinearLayout>

We have added EditText in xml file. Now, we will access this EditText in java file. Then, we will show Toast message, showing text entered in the EditText, on button click.

4. Access EditText in java file

Open src/main/java/com.tutorialwing.edittext/MainActivity.java file. Then, add below code into it.

package com.tutorialwing.edittext;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Button btnShow = findViewById(R.id.btnShow);
		if (btnShow != null) {
			btnShow.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					showText();
				}
			});
		}
	}

	private void showText() {
		EditText editText = findViewById(R.id.editText);
		if (editText != null) {
			Toast.makeText(this, editText.getText(), Toast.LENGTH_LONG).show();
		}
	}
}

Here, we are showing Toast message on button click that displays text entered in the android EditText.

Since AndroidManifest.xml file is important file in any android application, we are also going to mention it here.

AndroidManifest.xml file

Code inside main/AndroidManifest.xml file is as below.

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.edittext"
		  xmlns:android="http://schemas.android.com/apk/res/android">

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

When you run the application, you will get output as shown above.

That’s end of tutorial on Android EditText widget.

Leave a Reply