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.
Following are the important attributes related to EditText control. You can check Android official documentation for complete list of attributes and related methods which you can use to change these attributes are run time.
Inherited from android.widget.TextView Class −
android:autoText
If set, specifies that this TextView has a textual input method and automatically corrects some common spelling errors.
android:drawableBottom
This is the drawable to be drawn below the text.
android:drawableRight
This is the drawable to be drawn to the right of the text.
android:editable
If set, specifies that this TextView has an input method.
android:text
This is the Text to display.
Inherited from android.view.View Class −
android:background
This is a drawable to use as the background.
android:contentDescription
This defines text that briefly describes content of the view.
android:id
This supplies an identifier name for this view.
android:onClick
This is the name of the method in this View's context to invoke when the view is clicked.
This controls the initial visibility of the view.
This example will take you through simple steps to show how to create your own Android application using Linear Layout and EditText.
Following is the content of the modified main activity file src/com.example.demo/MainActivity.java. This file can include each of the fundamental lifecycle methods.
package com.example.demo;

import android.os.Bundle;
import android.app.Activity;

import android.view.View;
import android.view.View.OnClickListener;

import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
   EditText eText;
   Button btn;
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      eText = (EditText) findViewById(R.id.edittext);
      btn = (Button) findViewById(R.id.button);
      btn.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
            String str = eText.getText().toString();
            Toast msg = Toast.makeText(getBaseContext(),str,Toast.LENGTH_LONG);
            msg.show();
         }
      });
   }
}
Following will be the content of res/layout/activity_main.xml file −
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity" >
   
   <TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_alignParentTop="true"
      android:layout_marginLeft="14dp"
      android:layout_marginTop="18dp"
      android:text="@string/example_edittext" />
      
   <Button
      android:id="@+id/button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/textView1"
      android:layout_below="@+id/textView1"
      android:layout_marginTop="130dp"
      android:text="@string/show_the_text" />
      
   <EditText
      android:id="@+id/edittext"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/button"
      android:layout_below="@+id/textView1"
      android:layout_marginTop="61dp"
      android:ems="10"
      android:text="@string/enter_text" android:inputType="text" />

</RelativeLayout>
Following will be the content of  res/values/strings.xml to define these new constants −
<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">demo</string>
   <string name="example_edittext">Example showing EditText</string>
   <string name="show_the_text">Show the Text</string>
   <string name="enter_text">text changes</string>
</resources>
Following is the default content of  AndroidManifest.xml −
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.demo" >
      
   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name="com.example.demo.MainActivity"
         android:label="@string/app_name" >
      
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      
      </activity>
   </application>
   
</manifest>
Let's try to run your demo application. I assume you had created your AVD while doing environment setup. To run the app from Android studio, open one of your project's activity files and click Run  icon from the toolbar. Android Studio installs the app on your AVD and starts it and if everything is fine with your setup and application, it will display following Emulator window −
I will recommend to try above example with different attributes of EditText in Layout XML file as well at programming time to have different look and feel of the EditText. Try to make it editable, change to font color, font family, width, textSize etc and see the result. You can also try above example with multiple EditText controls in one activity.

    46 Lectures 
     7.5 hours 

 Aditya Dua

    32 Lectures 
     3.5 hours 

 Sharad Kumar

    9 Lectures 
     1 hours 

 Abhilash Nelson

    14 Lectures 
     1.5 hours 

 Abhilash Nelson

    15 Lectures 
     1.5 hours 

 Abhilash Nelson

    10 Lectures 
     1 hours 

 Abhilash Nelson
 Print
 Add Notes
 Bookmark this page