I am using autocomplete text view in my app.But my requirement is When i am editing some letters in autocomplete text view by pressing back space button, i want to do some action at that time.I am using below code but its not working.
actv.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
//You can identify which key pressed buy checking keyCode value with KeyEvent.KEYCODE_
if (keyCode == KeyEvent.KEYCODE_DEL) {
new RestaurantTask().execute("http://xxxx.co.in/xxxx_project/index.php/api/user/get_list/format/json",customerIdOnly);
}
return false;
}
});
Try this using textwatcher as follows.
Hope it helps. thanks
actv.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// perform your tasks here like
adapter.notifyDataSetChanged();
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
Related
I have 4 edittexts that act as a "enter your pin" field with each edittext set to only be able to take a single number before requesting focus to the next edittext. If I enter a number for the 1st pin, it then request focus to the 2nd, and then 3rd, etc.
What I am trying to do now is, if the user clicks on the 2nd edittext or 3rd and the 1st field has not been entered, it should go back and focus on the 1st field.
The edittext fields
I tried to do this using an onTouchListener and what I noticed is that after clicking on the 2nd edittext first instead of clicking on the left/1st edittext, I can see the focus going back to the first edittext but then immediately go back to the 2nd edittext almost a split second and ultimately still remain focus on the 2nd edittext.
I also tested using onClickListener. It does work but it takes two clicks. So I'll click on the 2nd edittext, nothing happens, and then I click it again which then moves the focus back to the 1st edittext but I need it to happen on the first click.
Here is part of the code
pin1EditTxt.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if(pin1EditTxt.getText().toString().length()==1) //size as per your requirement
{
pin2EditTxt.requestFocus();
}
}
});
// pin2EditTxt.setOnTouchListener(new View.OnTouchListener() {
// #Override
// public boolean onTouch(View v, MotionEvent event) {
// if(pin1EditTxt.length() == 0){
// pin2EditTxt.clearFocus();
// pin1EditTxt.requestFocus();
// }
// return false;
// }
// });
pin2EditTxt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(pin1EditTxt.length() == 0){
pin1EditTxt.requestFocus();
}
}
});
pin2EditTxt.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if(pin2EditTxt.getText().toString().length()==1) //size as per your requirement
{
pin3EditTxt.requestFocus();
}
}
});
pin3EditTxt.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if(pin3EditTxt.getText().toString().length()==1) //size as per your requirement
{
pin4EditTxt.requestFocus();
}
}
});
pin4EditTxt.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
//Hide keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
editTxtWrapper.requestFocus();
}
});
I guess you should be using pin1EditTxt.getText().toString().length()==0 instead of pin1EditTxt.length()==0 inside onclick.
I want to detect if something put into EditText was put there using SoftKey.
I've overridden dispatchKeyEvent but it doesn't work ...
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
Log.e("key pressed", String.valueOf(event.getKeyCode()));
return super.dispatchKeyEvent(event);
}
Find the solution
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
// like this you can detect
}
return super.onKeyDown(keyCode, event);
}
yourEditText.addTextChangedListener(watcher);
private TextWatcher watcher = new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if(!s.toString().isEmpty()){
//your text is not empty
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
};
I'm trying to make a function for when the "<"/less than key is pressed, but i cant seem to get it correctly. I tried to add onKey(View view, int i, KeyEvent event) with Log, so that it would log everytime the "<" key was pressed but no matter what nothing is logged. I set the on key listener on my edit text but it doesnt work. Help Please. Is there anything i must add?
postingET = (EditText) findViewById(R.id.postInput); postingET.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
Log.i("LOL", String.valueOf(view));
return false;
}
});
postingET.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
Change log to
Log.i("LOL", postingET.getText().toString());
I have a blank/empty edittext i want to know the "backspace listener" or "count zero listener" for now am using addTextChangedListener() but none of the method is getting executed when am pressing backSpace key because that is already empty.
editText.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if(s.length() != 0)
Field2.setText("");
}
});
I think you use TextWatcher
mEditText.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_DEL){
//on backspace
}
return false
}
});
I have a EditText. I wamt tp do something, when the user presses the Enter key while changing EditText. How can I do that?
The most simplest method:
final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
sample code for text watcher
your_edittext.addTextChangedListener(new InputValidator());
private class InputValidator implements TextWatcher {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
}
}
First, create an OnEditorActionListener (as a private instance variable, for example):
private TextView.OnEditorActionListener mEnterListener =
new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_DOWN) {
/* If the action is a key-up event on the return key, do something */
}
return true;
});
Then, set the listener (i.e. in your onCreate method):
EditText mEditText = (EditText) findViewById(...);
mEditText.setOnEditorActionListener(mEnterListener);
Another alternative is:
your_edittext.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
});
You need to use TextWatcher for this purpose. Here is an example on how to work with TextWatcher and Android textwatcher API.
your_edittext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
});