Just started with android, want to figure out keyUp on edittext. I followed various tutorials on keyUp it works but only on pressing back button. I want it to work on type. Just like phonebook, as you type some event will be triggered.
My code:
private EditText input;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
addKeyListener();
}
public void addKeyListener() {
input = (EditText) findViewById(R.id.names);
input.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Toast.makeText(Contacts.this,
input.getText() + " you have typed", Toast.LENGTH_SHORT)
.show();
return true;
}
});
}
Current code is only working on back button, if I press back button then the Toast
comes.
import android.text.TextWatcher;
. .
You can add a TextWatcher to your text field. In your case, this would look something like:
EditText in = new EditText(this);
in.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence cs, int s, int b, int c) {
Log.i("Key:", cs.toString());
}
public void afterTextChanged(Editable editable) { }
public void beforeTextChanged(CharSequence cs, int i, int j, int
k) { }
});
Best,