Check keyboard KeyEvent.KEYCODE_ENTER click from editText - java

I have an editText and I want to take the user to another Activity when the enter action of the keyboard is pressed. I tried adding this to the editText's xml:
android:imeOptions="actionSend"
And this is my code:
final EditText searchField = (EditText)findViewById(R.id.searchRecipe);
searchField.setImeActionLabel("Cerca", KeyEvent.KEYCODE_ENTER);
searchField.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEND) {
Intent goDetails = new Intent(RecipesList.this, RecipeDetails.class);
goDetails.putExtra("Keyword",searchField.getText());
startActivity(goDetails);
return true;
}
return false;
}
});
The enter button has its name changed but the action isn't fired. Can you help me understand where's the problem with the code?

Try this
final EditText searchField = (EditText)findViewById(R.id.searchRecipe);
searchField.setImeActionLabel("Cerca", KeyEvent.KEYCODE_ENTER);
searchField.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == KeyEvent.KEYCODE_ENTER) {
// do stuff here
}
return false;
}
});
Edited
KeyEvents can also be listened this way.
searchField.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.KEYCODE_ENTER)
// do stuff here
return false;
}
});

Related

How to check for hold click Android studio/java

I want to make in my app when I hold click it does something and when I release the button it does something else. A variable will change to true when I hold and false when I release. How do I do that
(Android Studio)
Thanks
You have to use TouchListener and MotionEvent class. The MotionEvent class has Fields ACTION_UP, ACTION_DOWN, and ACTION_MOVE. You can use the getAction() method of the MotionEvent class.
Something like this
findViewById(R.id.btn).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// button pressed
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// button relased.
}
return false;
}
});
You can use the onTouchListener for your use case:
boolean isClicking = false;
myView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// the view is pressed
isClicking = true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// the view is released.
isClicking = true;
}
return false;
}
});

Make onTouchListener to update?

I haven't seen an answer for what I am trying to do. I want to update code for as long as I am pressing down on a button and when I let go, the code stops updating. Any advice helps please. Here's my code:
forward.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Code updates every frame
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP){
// Code stops updating
return true;
}
return true;
}
});
Here is the example:
button1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
text1.setText("Text1");
} else if (event.getAction() == MotionEvent.ACTION_UP) {
text1.setText("Text2");
}
return true;
}
});

onKeyListener not firing an event in Android

I have an Activity with one Fragment. This fragment has one button, and I want something to happen when I push the "enter" button on my hardware keyboard (I am testing it with adb keyevent).
I've read a lot of solutions here on stackoverflow, but none of them worked.
This is my fragment code:
btnPhoto = (Button) rootView.findViewById(R.id.taskBtnPhoto);
photoView = (ImageView) rootView.findViewById(R.id.taskPhotoView);
btnPhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
takePhoto();
}
});
btnPhoto.setFocusable(true);
btnPhoto.setFocusableInTouchMode(true);
btnPhoto.requestFocus();
btnPhoto.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (KeyEvent.KEYCODE_VOLUME_UP == keyCode) {
doFoo();
return true;
}
return false;
}
});
This code is placed in the onCreateView part of my fragment. Using debug, the event is never fired when I launch KeyEvent from ADB or use the hardware keys of my phone (volume keys).
public static class MyFragment extends Fragment {
#Override
public void onResume() {
super.onResume();
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
//YOUR CODE
return false;
}
});
}
}

controlling ime_action_done behaviour

I have two android activities with ime_action_done.
Activity A has it on EditText view.
There - the soft keyboard "done" closes the keyboard.
private void initLayout(){
mInputText.setImeOptions(EditorInfo.IME_ACTION_DONE);
}
no onEditorAction overriding
Activity B has it on a custom view that extends TokenCompleteTextView
(com.tokenautocomplete.TokenCompleteTextView extends android.widget.MultiAutoCompleteTextView implements android.widget.TextView$OnEditorActionListener )
There the done action does nothing.
completionView.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (completionView.enoughToFilter() && (adapter != null)) {
//some logic. doesn't separate action_done from the other actions.
}
return true;
}
});
How can i make the second one close the soft keyboard as well?
mInputText.setImeOptions(EditorInfo.IME_ACTION_DONE);
sets the action to be the default for action_done (close keyboard)
in Activity B i used setOnEditorActionListener which catches the ime_action and consumes it.
so i have had to add this in Activity B:
completionView.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
InputMethodManager imm =
(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(completionView.getWindowToken(), 0);
}
or like this:
completionView.setImeOptions(EditorInfo.IME_ACTION_DONE);
completionView.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//logic
}
return false;
}
});

java on editor action listner

Trying to use on action listner on a edittext using the following
editText = (EditText) findViewById(R.id.editTextnum);
editText.setOnEditorActionListener(new OnEditorActionListener()) ;
keep getting error "cannot instantiate the type textview.OnEditorActionListner
any ideas?
mark
UPDATE WORKING CODE HAS ERROR ON TOAST
editText = (EditText) findViewById(R.id.editTextnum);
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
EditText mEdit = (EditText)findViewById(R.id.editTextnum);
if(mEdit.length()==0){
Toast.makeText(this, "No Asset Number Entered", Toast.LENGTH_LONG).show();
}
else{
db.open();
Cursor c = db.getContact(mEdit.getText());
if (c.moveToFirst())
DisplayContact(c);
else
//Toast.makeText(this, "No contact found", Toast.LENGTH_LONG).show();
db.close();
}
}
return false;
}});
you haven't implemented the listener.
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
// do things
}
});

Categories

Resources