Android EditText Example

An EditText is an user interface element for entering and modifying text. Now let’s create an app which displays the text written in EditText, into a TextView.

Step 1: Create a new project with name edittextexample and package name com.myapp.edittextexample. Select File -> New -> New Project. Fill the forms and click “Finish” button.

Step 2: Open res -> layout -> xml (or) main.xml and add following code. Here we will create an EditText and a TextView in a RelativeLayout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="16dp"> 
 
    <EditText 
        android:id="@+id/editText" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_centerHorizontal="true" 
        android:hint="Enter your Name" 
        android:layout_marginTop="150dp" 
        android:inputType="text"/> 
 
    <TextView 
        android:id="@+id/textView" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="75dp" 
        android:layout_centerVertical="true" 
        android:layout_centerHorizontal="true" 
        android:textStyle="bold" 
        android:textColor="#000000"/> 
 
</RelativeLayout> 

Step 3: Open app -> java -> package and open MainActivity.java. Add following code in it. Here we will add a TextChangedListener to the EditText. When anything is written in EditText, we will convert that text to a String and display it in a TextView.

package com.myapp.edittextexample;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

private EditText editText;
private TextView textView;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        editText = (EditText) findViewById(R.id.editText);
        textView = (TextView) findViewById(R.id.textView);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            } 
           @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
            @Override
            public void afterTextChanged(Editable s) {
                String name = editText.getText().toString();
                textView.setText("Hi "+name);
            }
        });
    }

}

Output:

Now run the app and write something in the EditText field. The app will show contents of Edittext in the TextView below it.