Why is onKey() called twice?

Android

Android Problem Overview


I am using this code:

public boolean onKey(View v, int keyCode, KeyEvent event) {
msg = (EditText)findViewById(R.id.msg);
String message = msg.getText().toString();
			if(keyCode == 66)
			{
				//It's hitting here twice.			
			}
			return false;
		};

Can anyone please tell me why it's hitting twice when I press enter?

Android Solutions


Solution 1 - Android

OnKey is fired twice: the first time for key down, and the second time for key up, so you have to filter:

YOUR_VIEW.setOnKeyListener(new OnKeyListener()
        {                           
        	@Override
        	public boolean onKey(View v, int keyCode, KeyEvent event) {
        		
                //This is the filter
        		if (event.getAction()!=KeyEvent.ACTION_DOWN)
        			return true;
        		
            	switch (keyCode) {
                case KeyEvent.KEYCODE_1 : 
                	MakeToast(1);
                    break;
                case KeyEvent.KEYCODE_2 : 
                	MakeToast(2);
                    break;
                case KeyEvent.KEYCODE_3 : 
                	MakeToast(3);
                    break;
                                   
                }
            	
                return true;
            }

        });

Solution 2 - Android

 itemView.setOnKeyListener(new View.OnKeyListener() {
  @Override
  public boolean onKey(View v, int keyCode, KeyEvent event) {
    int pos = getAdapterPosition();
    if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_MENU){
     //code here
    }
    return false;
  }
 });

Solution 3 - Android

edittext.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View view, int i, KeyEvent keyEvent) {
            if (keyEvent.getAction() == KeyEvent.ACTION_DOWN || keyEvent.getAction() == KeyEvent.ACTION_UP) {
                switch (i) {
                    case KeyEvent.KEYCODE_ENTER:
                        if (!HelperFunction.isStringEmpty(edittext.getText().toString())) {

                            if (loadCount == 0) {

                                loadCount ++;

                                
                            }
                        }
                    default:
                        return false;
                }
            }
            return false;
        }

this code block prevents calling the code twice. I use loadCount String value , if loadCount == 0 run my code block and i am doing loadCount ++ in my if block. So My Code block work on time.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSPBView Question on Stackoverflow
Solution 1 - AndroidRedaxView Answer on Stackoverflow
Solution 2 - AndroidVictor Sam VSView Answer on Stackoverflow
Solution 3 - AndroidozanurkanView Answer on Stackoverflow