codeofaninja
website

EditText in Android: Example Codes, Screenshots and More!

Photo of Mike Dalisay
Modified Thursday, January 26, 2012
by - @ninjazhai
This post was updated May 4, 2013

Hi guys! Today I'm gonna show you some Android EditText examples that I usually use in my android applications. Android EditText is simply like a text field, text area or a text input field in online forms. It is an editable TextView.

In this post, we will cover:

1. Creating EditText Programatically

        1.1 How to Create an EditText Programatically.
2. EditText maxLength and digits Restrictions

3. Button-triggered EditText and Input Types

        3.1 How Get Value from an EditText Programatically.
        3.2 How to Assign Value to an EditText.
        3.3 How to Clear Value of an EditText.
        3.4 EditText Input Types with Screenshots and XML Layout Codes


                3.4.1 Plain Text Input
                3.4.2 Password Input
                3.4.3 Email Address Input
                3.4.4 Phone Number Input
                3.4.5 Number Input
                3.4.6 Signed Number Input
                3.4.7 Decimal Number Input

        3.5 Section 3 Complete Code
        3.6 Section 3 Code Download



1. Creating EditText Programatically

1.1 How to Create an EditText Programatically?

Android Code:
EditText editTextName = new EditText(this);
editTextName.setText("Mary");


2. EditText maxLength and digits Restrictions

2.1 Set Maximum Number of Characters that can be Entered to an EditText.

<!-- Set MaxLegth EditText -->
<TextView
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Set MaxLegth of 5 for EditText:" />

<EditText
   android:id="@+id/editText3"
   android:layout_width="match_parent"
   android:layout_height="wrap_content" 
   android:maxLength="5">
</EditText>


2.2 Set EditText to accept numeric characters only. 

<!-- Allow digits 0,1,2,3,4,5 Only. 6, 7, 8, 9 is not allowed. -->
<TextView
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Allow Digits 0,1,2,3,4,5 Only:" />

<EditText
   android:id="@+id/editText4"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:digits="012345" />


3. EditText Listeners and Input Types

3.1 How Get Value from an EditText Programatically?
Tapping "Show Plain Text Input" button will show a Toast with value of our edittext for plain text input.

Android EditText Example

Android code:
final EditText editTextPlainTextInput = (EditText) this.findViewById(R.id.editTextPlainTextInput);

Toast.makeText( MainActivity.this, "Plain Text Input: " + editTextPlainTextInput.getText().toString(), Toast.LENGTH_SHORT).show();


3.2 How to Assign Value to an EditText?
Tapping "Assign Number Input to Plain Text Input" button will copy the value of our EditText for number input and put it to our EditText for plain text input.


Android code:
// plain text input
final EditText editTextPlainTextInput = (EditText) this.findViewById(R.id.editTextPlainTextInput);

// number input
final EditText editTextNumberInput = (EditText) this.findViewById(R.id.editTextNumberInput);

String number_value = editTextNumberInput.getText().toString();
editTextPlainTextInput.setText(number_value);


3.3 How to Clear Value of an EditText?
Tapping "Clear Plain Text Input" button will empty our EditText for plain text input and only show the "hint" we specified on the XML (in our example the hint is "Type who your are.").


Android code:
final EditText editTextPlainTextInput = (EditText) this.findViewById(R.id.editTextPlainTextInput);

editTextPlainTextInput.setText("");

3.4 EditText Input Types

3.4.1 Plain Text Input
Softkeyboard allows you to use any characters available.


XML code:
<EditText
    android:id="@+id/editTextPlainTextInput"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textViewPlainTextInput"
    android:hint="Type who you are."
    android:singleLine="true" >
</EditText>

3.4.2 Password Input
Softkeyboard allows you to use any characters available. The EditText field will show only black dots.


XML code:
<EditText
    android:id="@+id/editTextPasswordInput"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textViewPasswordInput"
    android:inputType="textPassword" >
</EditText>

3.4.3 Email Address Input
Softkeyboard shows the @ and .com buttons this time.


XML code:
<EditText
    android:id="@+id/editTextEmailAddressInput"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textViewEmailAddressInput"
    android:inputType="textEmailAddress" />

3.4.4 Phone Number Input
Softkeyboard allows you to enter numbers and any characters acceptable for phone numbers like *, #, -, etc.


XML code:
<EditText
    android:id="@+id/editTextPhoneNumberInput"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textViewPhoneNumberInput"
    android:inputType="phone" />

3.4.5 Number Input
Softkeyboard allows you to enter only numbers this time.


XML code:
<EditText
    android:id="@+id/editTextNumberInput"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textViewNumberInput"
    android:inputType="number" />

3.4.6 Signed Number Input
Softkeyboard allows you to enter numbers and negative sign.


XML code:
<EditText
    android:id="@+id/editTextSignedNumberInput"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textViewSignedNumberInput"
    android:inputType="numberSigned" />

3.4.7 Decimal Number Input
Softkeyboard allows you to enter only decimal numbers.


XML code:
<EditText
    android:id="@+id/editTextDecimalNumberInput"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textViewDecimalNumberInput"
    android:inputType="numberDecimal" />

3.5 Section 3 Complete Code
Here's the complete code for section 3.

MainActivity.java - includes the button click listener to perform actions with the EditText.
package com.example.androidedittextexample;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

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

        try {

            // plain text input
            final EditText editTextPlainTextInput = (EditText) this.findViewById(R.id.editTextPlainTextInput);
            
            // number input
            final EditText editTextNumberInput = (EditText) this.findViewById(R.id.editTextNumberInput);

            View.OnClickListener handler = new View.OnClickListener() {
                public void onClick(View v) {
                    // we will use switch statement and just
                    // get thebutton's id to make things easier
                    switch (v.getId()) {

                    // toast will be shown with the
                    // EditText for plain text input
                    case R.id.ShowPlainTextInput:
                        Toast.makeText( MainActivity.this, "Plain Text Input: " + editTextPlainTextInput.getText().toString(), Toast.LENGTH_SHORT).show();
                        break;

                    // the value of EditText for number input
                    // will be the value of EditText for plain text input
                    case R.id.AssignToPlainTextInput:
                        String number_value = editTextNumberInput.getText().toString();
                        editTextPlainTextInput.setText(number_value);
                        break;

                    // the EditText for plain text input will be cleared
                    case R.id.ClearPlainTextInput:
                        editTextPlainTextInput.setText("");
                        break;
                    }
                }
            };

            // we will set the listeners of our three buttons
            findViewById(R.id.ShowPlainTextInput).setOnClickListener(handler);
            findViewById(R.id.AssignToPlainTextInput).setOnClickListener(handler);
            findViewById(R.id.ClearPlainTextInput).setOnClickListener(handler);

        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}


activity_main.xml - XML layout with EditText and buttons.

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

    <!-- we're using scroll view to see all contents -->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <!-- EditText for plain text input -->

            <TextView
                android:id="@+id/textViewPlainTextInput"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="EditText For Plain Text Input:" />

            <EditText
                android:id="@+id/editTextPlainTextInput"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textViewPlainTextInput"
                android:hint="Type who you are."
                android:singleLine="true" >
            </EditText>

            <!-- EditText for password input -->

            <TextView
                android:id="@+id/textViewPasswordInput"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/editTextPlainTextInput"
                android:text="EditText For Password Input:" />

            <EditText
                android:id="@+id/editTextPasswordInput"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textViewPasswordInput"
                android:inputType="textPassword" >
            </EditText>

            <!-- EditText for email address input -->

            <TextView
                android:id="@+id/textViewEmailAddressInput"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/editTextPasswordInput"
                android:text="EditText For Email Address Input" />

            <EditText
                android:id="@+id/editTextEmailAddressInput"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textViewEmailAddressInput"
                android:inputType="textEmailAddress" />

            <!-- EditText for phone number input -->

            <TextView
                android:id="@+id/textViewPhoneNumberInput"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/editTextEmailAddressInput"
                android:text="EditText For Phone Number Input" />

            <EditText
                android:id="@+id/editTextPhoneNumberInput"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textViewPhoneNumberInput"
                android:inputType="phone" />

            <!-- EditText for number input -->

            <TextView
                android:id="@+id/textViewNumberInput"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/editTextPhoneNumberInput"
                android:text="EditText For Number Input:" />

            <EditText
                android:id="@+id/editTextNumberInput"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textViewNumberInput"
                android:inputType="number" />

            <!-- EditText for signed number input -->

            <TextView
                android:id="@+id/textViewSignedNumberInput"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/editTextNumberInput"
                android:text="EditText For Signed Number Input:" />

            <EditText
                android:id="@+id/editTextSignedNumberInput"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textViewSignedNumberInput"
                android:inputType="numberSigned" />

            <!-- EditText for decimal number input -->

            <TextView
                android:id="@+id/textViewDecimalNumberInput"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/editTextSignedNumberInput"
                android:text="EditText For Decimal Number Input:" />

            <EditText
                android:id="@+id/editTextDecimalNumberInput"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textViewDecimalNumberInput"
                android:inputType="numberDecimal" />

            <!-- Some options with EditText -->

            <!-- Button to clear plain text input -->
            <Button
                android:id="@+id/ClearPlainTextInput"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/editTextDecimalNumberInput"
                android:text="Clear Plain Text Input" />

            <!-- Button to assign number input to plain text input -->
            <Button
                android:id="@+id/AssignToPlainTextInput"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/ClearPlainTextInput"
                android:layout_below="@+id/ClearPlainTextInput"
                android:text="Assign Number Input to Plain Text Input" />

            <!-- Button to show plain text input -->
            <Button
                android:id="@+id/ShowPlainTextInput"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/AssignToPlainTextInput"
                android:layout_below="@+id/AssignToPlainTextInput"
                android:text="Show Plain Text Input" />
        </RelativeLayout>
    </ScrollView>

</RelativeLayout>


3.6 Section 3 Code Download
Code used in this section can be downloaded here:

The Code of a Ninja Resources


For FREE programming tutorials, click the red button below and subscribe! :)
Thanks for the comments!
 
 
Fundamentals
"First do it, then do it right, then do it better."
~ Addy Osmani
"Talk is cheap. Show me the code."
~ Linus Torvalds
Let's Stay Connected!
g+ r
Android app on Google Play
© 2011-2014 The Code Of A Ninja. All rights reserved. Proudly Powered by Google Blogger. Images, logos, marks or names mentioned herein are the property of their respective owners.