Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Controlling Input with KeyListeners

Author: Pratik Rupwal
Published? true -- FormatLanguage: W

Problem:

My application contains a few text boxes in which I want to restrict the users to enter only numbers; also in some cases to only positive numbers or even integers as well as dates. How do I achieve this with minimum efforts?

Solution:

Android provides KeyListener classes to help you restrict the users to enter only numbers/positive numbers/integers/positive integers and much more.

Discussion:

Android.text.method package includes an interface 'KeyListener' and some classes like DigitsKeyListener,DateKeyListener, etc. which implement this interface.

Below is a sample application which demonstrate few of them:

This layout file creates 5 textviews and 5 edittexts textviews display the input type allowed for their respective edittexts.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    
	<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/textview1"
    android:text="digits listener with signs and decimal points"
    />
    <EditText  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/editText1"
	/>

	<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/textview2"
    android:text="digits listener without signs and decimal points"
    />
    <EditText  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/editText2"
	/>
	
    <TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/textview3"
    android:text="date listener"
    />
    <EditText  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/editText3"
	/>
	
    <TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/textview4"
    android:text="multitap listener"
    />
    <EditText  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/editText4"
	/>
	
    <TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/textview5"
    android:text="qwerty listener"
    />
	<EditText  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/editText5"
	/>
</LinearLayout>

Below is the code for the activity which restricts the edittext input to numbers/positiveintgers/etc.(Refer the comments for group of keys allowed):

import android.app.Activity;
import android.os.Bundle;
import android.text.method.DateKeyListener;
import android.text.method.DigitsKeyListener;
import android.text.method.MultiTapKeyListener;
import android.text.method.QwertyKeyListener;
import android.text.method.TextKeyListener;
import android.widget.EditText;

public class KeyListenerDemo extends Activity {
    /** Called when the activity is first created. */   
@Override  
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState);
	setContentView(R.layout.main);
	EditText editText1=(EditText)findViewById(R.id.editText1);
        DigitsKeyListenerdigkl1=DigitsKeyListener.getInstance(true,true);//allows digits with positive/negative signs and decimal points
        editText1.setKeyListener(digkl1);
        
        EditText editText2=(EditText)findViewById(R.id.editText2);
        DigitsKeyListener digkl2=DigitsKeyListener.getInstance();//allows positive inetger only(no decimal values allowed)
        editText2.setKeyListener(digkl2);
        
        EditText editText3=(EditText)findViewById(R.id.editText3);
        DateKeyListener dtkl=new DateKeyListener();//allows date only
        editText3.setKeyListener(dtkl);
        
        EditText editText4=(EditText)findViewById(R.id.editText4);
        MultiTapKeyListener multitapkl=new MultiTapKeyListener(TextKeyListener.Capitalize.WORDS,true);//allows multitap with 12-key keypad layout.
        editText4.setKeyListener(multitapkl);
        
        EditText editText5=(EditText)findViewById(R.id.editText5);
        QwertyKeyListener qkl=new QwertyKeyListener(TextKeyListener.Capitalize.SENTENCES,true);//allows qwerty layout for typing.
        editText5.setKeyListener(qkl);
    }
}

For using 'MultiTapKeyListener' your phone should support the 12-key layout and it needs to be activated. To activate this 12-key layout, go to setting->Language and Keyboard -> On screen Keyboard layout -> select 'Phone layout' options.

the above options could be 'Qwerty layout'/'Phone layout'/'Compact qwerty layout'.

See Also:

Class Usage
BaseKeyListener Abstract base class for key listeners.
DateTimeKeyListener For entering dates and times in the same text field.
MetaKeyKeyListener This base class encapsulates the behavior for tracking the state of meta keys such as SHIFT,ALT and SYM as well as the pseudo-meta state of selecting text.
NumberKeyListener For numeric text entry.
TextKeyListener This is the key listener for typing normal text.
TimeKeyListener For entering times in a text field.

In Constrain EditText Values with Attributes and the TextWatcher Interface we mention that you can constrain values in certain cases simply by using the android:inputType attribute.

GR8DAN 2011-09-26 09:49:05.585 Use of attribute android:inputType can restrict text without any coding.