Hot questions for Using Android EditText in android softkeyboard

Top Java Programmings / Android EditText / android softkeyboard


I have an autocomplete edittext field that sends queries to a server everytime the text changes, however if the user types a lot of text and then holds down "delete" I don't want to send a query for every single character deleted. One way of getting around this would be to cache all the queries as the characters were entered and then play back the cache on deletion - but that seems to me to be unnecessarily complicated. An easier way is to detect keydown/keyup and then only send a query if the text has changed but the user also isn't touching the keyboard. Which leads me to the difficulty of how to catch keyup from a softkeyboard.

I've already tried ontouchlistener (doesn't fire) and onkeypressedlistener (also doesnt fire) and even overriding onKeyPreIme inside the edittext (also doesn't fire). Is there a way of doing this?


The way I fixed this problem is to add a delay to the query so it won't execute every time. My code looked something like this:

long lastPress = 0l;
public void onTextChanged(CharSequence s,
         int start, int before, int count){
    if(System.currentTimeMillis() - lastpress > 500){
        lastPress= System.currentTimeMillis();
        // insert query here

Hope this helps.


There is no XML associated with this EditText. It's generated in the Activity.

final EditText txtPrice = new EditText(getContext());
txtPrice.setKeyListener(DigitsKeyListener.getInstance(false, false));
txtPrice.setFilters(new InputFilter[] { new InputFilter.LengthFilter(3) });

This is okay, but not ideal. I want to show the Android numeric keyboard, to really simplify things.

How do I call that?


You should set your EditText input type

final EditText txtPrice = new EditText(getContext());
txtPrice.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER);

This will restrict its inputs to be numbers only, and thus, Android will automatically show only the numeric keyboard.