17

I'm creating a search option in an android application.

After pressing "Search" on the screen-keyboard an event triggers which sends the query to the server. What I want to do is disable the "Search" button on the screen-keyboard when no text has been typed in it's corresponding EditText yet.

I added this to the EditText:

android:imeOptions="actionSearch"

So that's why the keyboard has got a "Search" button in stead of the default "enter"/"done". (Just in case you where wondering).

Pieter888
  • 4,882
  • 13
  • 53
  • 74

7 Answers7

19

We cannot disable the action button on the android keyboard. You will have to settle with checking for empty string in editor action listener.

Ron
  • 24,175
  • 8
  • 56
  • 97
  • That's to bad. I guess checking for an empty string will have to do. I'll choose this answer because it's the real answer to my question, not just alternatives. – Pieter888 May 14 '12 at 12:07
3
setOnEditorActionListener(new TextView.OnEditorActionListener() {
if (txtView.getText().toString().trim().length() == 0) {
    Toast.makeText(getApplicationContext(), "Please Enter some text to search",
    Toast.Short).show();
    return true;    // returning true will keep the keyboard on
}
else search();
Abdul Saleem
  • 10,098
  • 5
  • 45
  • 45
0

try this (may not be the exact result you wanted, but might be the closest you can get.

1)Remove the android:imeOptions="actionSearch" from the xml

2)Create a custom TextWatcher addTextChangedListener(TextWatcher watcher) that allows you to change the keyboard dynamically something like this

textMessage = (EditText)findViewById(R.id.textMessage);
textMessage.addTextChangedListener(new TextWatcher(){
        public void afterTextChanged(Editable s) {
            if(firstTime){//a boolean you init to true
            firstTime = false;
            textMessage.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after){}
        public void onTextChanged(CharSequence s, int start, int before, int count){}
    });
MikeIsrael
  • 2,871
  • 2
  • 22
  • 34
  • I tried, but it's not possible to change a button's `ImeOptions` after the view has been created. But it doesn't feel like the right solution anyway because I want the enter/search-button to be disabled until something has been typed. If this answer worked, it would show the the text "Done" on the enter-button instead of "Search". I just want the button to be grayed-out or something to show the user he/she cannot so a search on an empty string. – Pieter888 May 10 '12 at 11:18
0

you need to make Custom EditText ,Make Extends EditText and @Override Below Method in it.

Here is example code.

private ArrayList<Integer> keysToIgnore = new ArrayList<Integer>();

    public EditTextCus(Context context) {
        super(context);
    }

    public EditTextCus(Context context, AttributeSet attrs){
        super(context, attrs);      
    }

    public EditTextCus(Context context, AttributeSet attrs, int defStyle){
        super (context, attrs, defStyle);       
    }



private Boolean keyInIgnoreList(int keyCode) {      
        for (Integer i:keysToIgnore){
            int key = i.intValue();
            if (key == keyCode)
                return true;
        }
        return false;
    }

    public void addKeyToIgnoreList(int keyCode) {

        if (!keyInIgnoreList(keyCode)){
            keysToIgnore.add(keyCode);
        }
    }

    public void removeKeyFromIgnoreList(int keyCode) {

        if (keyInIgnoreList(keyCode)){
            Integer key=new Integer(keyCode);
            keysToIgnore.remove(key);
        }
    }


@Override
    public boolean dispatchKeyEventPreIme(KeyEvent event) {
        //Log.d("ws", "dispatchKeyEventPreIme(" + event + ")");

            // swallow the any key in the ignore list
            if (keyInIgnoreList(event.getKeyCode())) {
                KeyEvent.DispatcherState state = getKeyDispatcherState();
                if (state != null) {
                    if (event.getAction() == KeyEvent.ACTION_DOWN
                            /*&& event.getRepeatCount() == 0*/) {
                        state.startTracking(event, this);
                        return true;
                    } else if (event.getAction() == KeyEvent.ACTION_UP
                            && !event.isCanceled() && state.isTracking(event)) {
                        return true;
                    }
                }
            }
        }
        return super.dispatchKeyEventPreIme(event);
    }

you can also do this to Search Key :

mCustomEditText.setOnEditorActionListener(new OnEditorActionListener() {

            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(actionId==EditorInfo.IME_ACTION_SEARCH && event.getAction()==KeyEvent.KEYCODE_SEARCH){
                    System.out.println("Search pressed.........");
                }
                return false;
            }
        });
Herry
  • 7,037
  • 7
  • 50
  • 80
  • Even after adding both the keyCode from enter (66) and search (84) it still doesn't ignore it. After uncommenting the line where you log the event I found out it never reaches the `dispatchKeyEventPreIme`. Nothing is outputted to the console. – Pieter888 May 10 '12 at 12:05
  • @Pieter888 when hard key of Search like in HTC phone you will see `dispatchKeyEventPreIme` this method will call and for receive softkeyboard Event you can try this `mCustomEditText.setOnEditorActionListener` as updated in answer.Try in this method for enter key for softkeyboard. – Herry May 10 '12 at 12:41
0

Implement OnKeyListener in your Activity.

add this keyListener to your EditText.

 myEditText.setOnKeyListener(this);  

override onKey() in your Activity.

@Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if(v.getId() == R.id.myEditText)
        {
            if(KeyEvent.KEYCODE_ENTER == keyCode)
            {
                if(myEditText.getText().toString().equals("") || myEditText.getText().toString().equals(null))
                {
                    Toast.makeText(KeyboardTestActivity.this, "Event Eaten", Toast.LENGTH_SHORT).show();
                    Log.e("onKey", "Event Eaten");
                    return true;
                }
            }
        }
        return false;
    }

and this is Done it will Display a Toast whenever your EditText is empty and you press search Key.

Note: onKey() gets called twice once for keyDown and then for KeyUp. you will have to filter it using KeyEvent instance, received as a parameter in onKey() method.

N-JOY
  • 10,344
  • 7
  • 51
  • 69
-1
    SearchInput.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            if( SearchInput.getText().toString().length() == 0 )
            {
                Search.setClickable(false);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub
            Search.setClickable(false);


        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub
            Search.setClickable(true);
        }

    });

or Try this..! for fancy error dilog

    Search.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            if( searchinput.getText().toString().length() == 0 )
            {
                searchinput.setError( "Enter something Blah Blah..!" );
            }
            else
            {


       // Original Functions Goes Here
            }

Use the following Code in Edit Text,

android:imeOptions="flagNoEnterAction"

I am not able to set flagNoEnterAction = "false" But someone can help you

Ron
  • 24,175
  • 8
  • 56
  • 97
VenomVendor
  • 15,064
  • 13
  • 65
  • 96
  • This only works if the search button was a button I made myself in the app. But I'm talking about disabling the enter/search-key on the android-keyboard. – Pieter888 May 10 '12 at 11:11
  • I won't upvote this answer because it's not a valid answer to my question. I want to disable the android's on-screen keyboard's search button. Not a button in my application. – Pieter888 May 10 '12 at 11:28
  • Answer Edited , 3rd code added, might help you or someone else atleast – VenomVendor May 10 '12 at 12:24
-1

Attach OnEditorActionListener to your text field and return true from its onEditorAction method, when actionId is equal to IME_ACTION_DONE. This will prevent soft keyboard from hiding:

EditText txtEdit = (EditText) findViewById(R.id.txtEdit);
txtEdit.setOnEditorActionListener(new OnEditorActionListener() {

  public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    if (actionId == EditorInfo.IME_ACTION_DONE) {
      // your additional processing... 
      return true;
    } else {
      return false;
    }
  }
});

Refer this LINK LINK

Community
  • 1
  • 1
Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64