How do I trigger an action when the user has hit enter? - android

If (in Android) I have an EditText box, how can I trigger an event when the user has finished entering data and hits return/Next?
I have tried using the code below but it seems to have no effect. I also get an 'The method onEditorAction(EditText, int, KeyEvent) from the type new extView.OnEditorActionListener(){} is never used locally' error.
myEditText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
public boolean onEditorAction(EditText v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT)
{

I played around a bit with various things and found that the code below works:
myEditText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
// Do some stuff
}
});

I believe you want is the setOnEditorActionListener() method, dev guide info here on the method.

Add a KeyListener to the textbox that listens key events. Within the key event, listen to the "ENTER" key pressed and perform your action.
You can find something similar here: Android Development: How To Use onKeyUp? and in the docs for UI-Events.
A good example is in the linked SO question. Following this, you should get the desired result.

Related

How to remove the hability to focus next EditText when we push the return key?

When I'm writing in an EditText, if I push the next/done key then the focus will move to the next EditText. How to forbid this behaviour (programmatically)?
Just setFocusable(true) to the mother view of your edittext
You may try this code :
yourEditText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT) {
// do something with next printed on your editText
}
return true;
}
});
You can manage cases for different option like other action(Done/Next/Search etc.) too.

How to load URL from Keyboard ?

I am trying to load an URL from the keyboard, such that when I click on "DONE" the URL loads in WebView. I tried to use KeyEvent but it is not working. How do I do it?
Assuming that you are typing something into an edittext then the following can be used to do what you want:
yourEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
yourWebview.loadUrl(yourEditText.getText());
return true;
}
return false;
}
});
You can have different action depending on what the keyboard says for example "next" or "done", just check the other actions inside EditorInfo.
Let me know if you need any other help.

android: onKeyListener is not fired for ENTER key pressed for multiline EditText

I've found a lot of similar questions but all of them don't work for multilines EditText.
If i set inputType="text" EditText becomes singleline and returns are just not shown. I also need "Enter" button to be "Enter" button (not "Next" or "Done").
content.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN &&
keyCode == KeyEvent.KEYCODE_ENTER)
doSmthSpecial();
return false;
}
});
I don't know why it is not invoking ENTER key event. but you can go for another method which serves you same purpose. use OnEditorActionListener.
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
doSomethingSpecial();
return true;
}
});
this method is invoked when you press ENTER key on EditText.
i have also digged alot of time on internet for this question's solution then,i found this:
et.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
et is you edit text.
this,worked like charm for me.

Event when user select back?

How can I catch the event when the user, after he entered the text on an EditText, select Back and the keyboard disappear?
Override onBackPressed at your Activity class:
#Override
public void onBackPressed() {
//If keyboard is on, do what you want
super.onBackPressed();
}
If you want to catch the back key when user has finished entering text in EditText and he presses back key then you should use:
EditText edit = (EditText)findViewById(R.id.yourId);
edit.setOnKeyListener(new EditText.OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
System.out.println("******back key caught in edit.setOnKeyListener");
}
return false;
}
});
I had the same problem. Vineet Shukla's answer was not working, until I made sure that the delegate was a EditText.OnKeyListener. Prior to that it was a View.OnKeyListener and I was not seeing KeyEvent.KEYCODE_BACK == keyCode, for was I even seeing onKey ever called. I hope this is helpful to someone having a similar problem, although this post is a year old. Cheers.

How to watch multiple edittext boxes for a softkey enter or done press?

I have 14 edittext boxes that a user can change at will. When the text is changed in one of them, a softpad key press on the 'enter/next/done' key should re-run a calculation using the new text. I've tried onKey listener but it doesn't work on the soft keyboard, on;y the hard keypad. I've tried a textwatcher like onTextChanged but it reacts and runs the calculation when only a single digit is entered, before the user can input a two or more digit number. So... I hear the onEditorActionListener works on soft keypads to watch for the keypress, but I can't get the syntax right. Here's what I have:
In the onCreate method:
myEdittext1.setOnEditorActionListener(this);
...
myEdittext14.setOnEditorActionListener(this);
Then, outside the onCreate method, I have:
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{ if (event.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION)
{ //do my calcs}
}
return(true);
}
The code gives me the foreced-close business. Any help would be greatly appreciated.
KeyEvent can be null. You need to check for this situation in your listener.
v The view that was clicked.
actionId Identifier of the action. This will be either the identifier you supplied, or EditorInfo.IME_NULL if being called due to the enter key being pressed.
event If triggered by an enter key, this is the event; otherwise, this is null.
onEditorAction is the appropriate way to listen for finish and Done soft keyboard actions.
so here's a working version:
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{ if (event == null || event.getAction()==KeyEvent.ACTION_UP)
{ //do my calcs
return(true);//reset key event for next press
}
}
The enter key returns a 'null' in the event variable, thats why my version at the top of this post crashed.
Nick's code above worked well too, just remember to set the xml property android:imeOptions=actionDone" for all of the edittext boxes.

Categories

Resources