Every time I pressed enter the toast keeps popping out even if it should not come out…
take a lot at my code:
etAddMove.setOnKeyListener(new OnKeyListener() {
@SuppressLint("NewApi")
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_ENTER && event.getRepeatCount() == 0){
String ssmoveName = etAddMove.getText().toString();
int x = ssmoveName.length() - 1;
if (ssmoveName.isEmpty() || Character.isWhitespace(ssmoveName.charAt(0)) || Character.isWhitespace(ssmoveName.charAt(x))) {
Toast.makeText(ListMovingNames.this,
"Please enter a valid name! Avoid giving a blank name or white space at the beginning or end of the name",
Toast.LENGTH_LONG).show();
}else{
try {
SQLHandler check = new SQLHandler(ListMovingNames.this);
check.open();
String scheck = check.checkMove(ssmoveName);
check.close();
if (!scheck.equals(ssmoveName)) {
Toast.makeText(ListMovingNames.this, "Move name already exist please give a different name", Toast.LENGTH_LONG).show();
} else{
SQLHandler entry = new SQLHandler(ListMovingNames.this);
entry.open();
entry.createMove(ssmoveName);
entry.setTodo(ssmoveName);
entry.close();
Intent i = new Intent(getApplicationContext(), StartMoving.class);
i.putExtra("moveName", ssmoveName);
startActivity(i);
}
} catch (Exception e) {
// TODO Auto-generated catch block
SQLHandler entry = new SQLHandler(ListMovingNames.this);
entry.open();
entry.createMove(ssmoveName);
entry.setTodo(ssmoveName);
entry.close();
Intent i = new Intent(getApplicationContext(), StartMoving.class);
i.putExtra("moveName", ssmoveName);
startActivity(i);
}
}
return true;
}
return onKeyDown(keyCode, event);
}
});
how can you make this guy stop if I don’t want it to show…
EDIT
I think I know why this is happening, It must be because each time i press enter keyup and keydown is initialize thats why the first initialization is keydown which calls the condition and returns false and when keydown is called the condition is called again which will return true, making the toast display. thats what i think so far…
Finally solve this shit. As I was suspecting the reason why toast keeps showing is that everytime the enter key is press, the keydown and keyup is called that’s why the process is called twice making the toast appear because the condition to show the toast is meet when the process is called the second time.
So to avoid that
KeyEvent.ACTION_DOWNmust be present.