Browse By

Android EditText Example

Hello friends…
Create EditText Example in Android Studio 2.0.

Sometimes, we are forced to limit the number of characters allowed to enter by end-user in a Textbox in our Android based applications. Twitter status box, Facebook Status box, and default SMS Draft  are the great examples of that.

 In android to get user input in Text Box , you can use “android.widget.EditText”. To listen user input you can use addTextChangedListener() Inherited Method of EditText.

In this tutorial, we will create the simple android application which will behave like Twitter Status box. We will display a number of character remaining just below to Status Box and will change color accordingly to make a user aware about status character count. Below is the summary of the task which we will cover in this tutorial.

  1. Display Twitter like Status box using EditText
  2. Observer Number of character entered and remains.

Direct Download Full Source Code



Android EditText Example

Create New Android Project

  1. Create a new project and fill the required details File ⇒ New ⇒ Android Project
  2. Open your main.xml and design a simple layout for Status box and Remaining character counts.
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android=""
    	android:orientation="vertical" android:layout_width="fill_parent"
    	android:layout_height="fill_parent" android:background="#fff">
    	<TextView android:id="@+id/textView1" android:layout_height="wrap_content"
    		android:textSize="20dp" android:layout_width="match_parent"
    		android:text="What's happening ?" android:textColor="#000066"></TextView>
    	<EditText android:layout_width="match_parent"
    		android:layout_height="match_parent" android:layout_weight="1"
    	<LinearLayout android:id="@+id/linearLayout1"
    		android:layout_width="match_parent" android:layout_height="wrap_content">
    		<TextView android:id="@+id/lblCount" android:text="140"
    			android:layout_gravity="right" android:layout_width="wrap_content"
    			android:paddingRight="10dp" android:textSize="15dp"
    			android:layout_weight="1" android:layout_height="match_parent"
    			android:gravity="right|center" android:textColor="#0f0"></TextView>
    		<Button android:id="@+id/button1" android:layout_height="wrap_content"
    			android:layout_width="wrap_content" android:text="Tweet"
    			android:layout_gravity="right" android:layout_weight="0.25"></Button>
  3. Now open your Activity Class and put the following code.
    package com.vrs.pulse7status;
    import android.os.Bundle;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    public class Pulse7StatusActivity extends Activity {
    	//Widget GUI
    	EditText txtStatus;
    	TextView lblCount;
    	Button btnTweet;
    	// Init Static Members
    	static int MAX_COUNT = 140;
        /** Called when the activity is first created. */
        public void onCreate(Bundle savedInstanceState) {
            // Init Widget GUI
            txtStatus = (EditText)findViewById(;
            lblCount = (TextView)findViewById(;
            // Attached Listener to Edit Text Widget
            txtStatus.addTextChangedListener(new TextWatcher() {
    			public void onTextChanged(CharSequence s, int start, int before, int count) {
    				// TODO Auto-generated method stub
    			public void beforeTextChanged(CharSequence s, int start, int count,
    					int after) {
    				// TODO Auto-generated method stub
    			public void afterTextChanged(Editable s) {
    				// Display Remaining Character with respective color
    				int count = MAX_COUNT - s.length();
    				if(count < 10)
    				if(count < 0)
  4. Now Execute Application
    Android EditText Example

    When application launch, it display 140 remaining characters.

    Android EditText Example

    When you type in the status box, it will automatically reduce remaining characters.


    Android Edittext Example

    If you type more than 140 character remaining character color becomes red automatically !!!


Points to Remember

  • addTextChangedListener() method is used to listen changes in EditText
  •  afterTextChanged() method of  TextWatcher is used to check a number of characters has been entered in Edit Text.

I hope you like this article. Share your views to improve content.

Download Full Source Code