Tuesday 28 May 2013

Android EditText Example

In Android, you can use “EditText” class to create an editable textbox to accept user input.

A text field allows the user to type text into your app. It can be either single line or multi-line. Touching a text field places the cursor and automatically displays the keyboard. In addition to typing, text fields allow for a variety of other activities, such as text selection (cut, copy, paste) and data look-up via auto-completion.

You can add a text field to you layout with the EditText object. You should usually do so in your XML layout with a <EditText> element.

Specifying the Keyboard Type


Text fields can have different input types, such as number, date, password, or email address. The type determines what kind of characters are allowed inside the field, and may prompt the virtual keyboard to optimize its layout for frequently used characters.
You can specify the type of keyboard you want for your EditText object with the android:inputType attribute. For example, if you want the user to input an email address, you should use the textEmailAddress input type:


<EditText
android:id="@+id/edEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress"/>
This tutorial show you how to create a edittext in XML file, and we can  display message typed in the edittext on TextChangeListner of edittext.

1. EditText

Open “res/layout/activity_main.xml” file, add a “EditText” component.
<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"
tools:context=".MainActivity"
android:orientation="vertical" >

<EditText
android:id="@+id/edText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
</LinearLayout>

2. EditText Listener
Attach a TextChange listener inside your activity “onCreate()” method, to monitor following events .
we are showing a message on the screen whenever a text in edittext is changed.

package com.learnsimply.edittextsample;

import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
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);
EditText edText = (EditText)findViewById(R.id.edtext);
edText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
}


}

Run the application





Download Source Code Edittextexample.zip

No comments:

Post a Comment