Android EditText Tutorial

Android EditText Tutorial

In this tutorial we will see how to access text entered by user in EditText and how to attach onKeyListener event to EditText. We will display a message in TextView if user press number 3 or back button.

Video: Android EditText Example

Source code:
activity_text_box_demo.xml

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

<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=".TextBoxDemo" >

    <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="60dp"
        android:layout_marginTop="96dp"
        android:text="TextView" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="54dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

</RelativeLayout>

Source file TextBoxDemo.java

package com.example.textboxdemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.TextView;

public class TextBoxDemo extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_box_demo);
        addOnKeyListnerEvent();
    }
    public void addOnKeyListnerEvent()
    {
        final TextView tv = (TextView)findViewById(R.id.textView1);
        final EditText et = (EditText)findViewById(R.id.editText1);
        et.setOnKeyListener(new OnKeyListener(){

            @Override
            public boolean onKey(View arg0, int keycode, KeyEvent event) {
                // TODO Auto-generated method stub
if(event.getAction() == KeyEvent.ACTION_DOWN && keycode == KeyEvent.KEYCODE_3 )
{
    tv.setText("Pressed key is 3");
    }

if(keycode == KeyEvent.KEYCODE_BACK)
{
tv.setText("Back key is pressed");   
}

                return false;
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.text_box_demo, menu);
        return true;
    }

}

Android screen navigation example using Activity and IntentAndroid WebView ExampleAndroid Tutorial For BeginnersAndroid Button ClickAndroid Button onClickListener ExampleBest Android ApplicationsHow to earn money with android phoneHow to remove android applicationsAndroid install APK from computer applicationAndroid Market Loginremove android apps

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.