My Life with Android
Introduction Android Componets UserInterface Advanced UI Data Storage Advanced Concepts Others New Studio

Android EditText Control

A EditText is an overlay over TextView that configures itself to be editable. It is the predefined subclass of TextView that includes rich editing capabilities.It is used to accept user input.

Importent Methods

  1. getText().toString()
  2. setText (CharSequence text)
Example :

Basic Syntax :

<EditText
    android:id="@+id/edittext3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <requestFocus />
</EditText>

<requestFocus /> - Request focus on a specified EditText view in startup.

Example - 1


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#E3F2FD"
    android:orientation="vertical"
    android:padding="8dp" >

    <EditText
        android:id="@+id/etUserName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:background="#B39DDB"
        android:ems="10"
        android:hint="Please enter username"
        android:padding="8dp"
        android:textColor="#FFF" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/etPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:background="#B39DDB"
        android:ems="10"
        android:hint="Please enter password"
        android:padding="8dp"
        android:textColor="#FFF" />

</LinearLayout>

  • The em is simply the font size.
  • android:hint set hint text to display when the EditText is empty. The hint text will disappear once user enter any text.