I have an EditText which has the singleLine attribute set to true. When I press Enter on the keyboard, the keyboard is hidden. Is it possible to prevent this?
I've been using OnKeyListener which caused this problem. Switching to OnEditorActionListener stops the Keyboard from closing when pressing Enter and allows me to have full control of it.
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT || actionId == EditorInfo.IME_ACTION_DONE) {
//DO THINGS HERE
return true;
}
return false;
}
});
this should help you
youredittext.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int keyCode, KeyEvent event) {
if ( (event.getAction() == KeyEvent.ACTION_DOWN ) &&
(keyCode == KeyEvent.KEYCODE_ENTER) )
{
// hide virtual keyboard
InputMethodManager imm =
(InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInputFromInputMethod(edittext.getWindowToken(), 0);
return true;
}
return false;
}
});
when you press the enter key the inputMethodManager will show the keyboard, if needed.
hope this will solve your problem :)
edit:
if this will not work try use the event.getKeyCode() in the secend part of the if statment
edit II: sorry, i read it wrong, i fixed it now try this one.
Related
This is my code for an AlertDialog.Builder that has a custom view with an EditText. After entering the value inside the EditText, I want the press of Enter on the keyboard to act the same way as the PositiveButton of the AlertDialog.Builder. I have included the necessary 'imeOptions' part in the XML file. I manage to execute the code when pressing Enter, but the AlertDialog.Builder is still on screen and does not dismiss like when the PositiveButton on the AlertDialog.Builder is clicked.
//AlertDialog to set weekly income
incomeAlert = new AlertDialog.Builder(this);
incomeInflater = this.getLayoutInflater();
incomeDialogView = incomeInflater.inflate(R.layout.activity_weekincome, null);
incomeAlert.setView(incomeDialogView);
et_WeekIncome = incomeDialogView.findViewById(R.id.ls_WeekIncome);
et_WeekIncome.setOnEditorActionListener(new EditText.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
submitIncome();
return true;
}
return false;
}
});
incomeAlert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
submitIncome();
}
});
Thanks in advance for any help.
UPDATE: I managed to dismiss the AlertDialog.Builder by adding another piece of code as shown below
AlertDialog incomeDialog = incomeAlert.create();
incomeDialog.show();
Then when needing to dismiss, I use
incomeDialog.dismiss();
Since dismiss() is not available with AlertDialog.Builder, I had to create the Builder through an AlertDialog. Then I call dismiss() on the AlertDialog.
Thank you all for your input.
You can use OnKeyListener with your edit text to handle a specific keypress.
mEditTV.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
// do action
return true;
}
return false;
}
});
You can use the above setOnKeyListener this way.
et_WeekIncome.setOnKeyListener(new OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_ENTER:
submitIncome();
return true;
default:
break;
}
}return false;
}
});
Since dismiss() is not available with AlertDialog.Builder, I had to create the Builder through an AlertDialog. Then I call dismiss() on the AlertDialog.
There is another way to deal with this problem: use the setOnShowListener callback to set the key listener. This gives you access to the dialog's dismiss() method.
incomeAlert.setOnShowListener((DialogInterface d) -> {
et_WeekIncome.setOnKeyListener((v, keyCode, event) -> {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_ENTER) {
onClick(dialog, BUTTON_POSITIVE);
d.dismiss();
return true;
}
return false;
});
So i have a function that's supposed to listen for the enter key to be pressed and then hide the keyboard but when i tested it works on one device but not another.
My function:
notes.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() != KeyEvent.ACTION_DOWN) {
return false;
}
if (keyCode == KeyEvent.KEYCODE_ENTER) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getDecorView().getRootView().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
return true;
}
return false;
}
});
Works on one phone running Android 8.1 But not on my other phone running Android 8.0
What could possibly be causing this?
I have an activity with a numeric EditText sitting above a button.
I have noticed that if you click in the edit text and use the hard keyboard to type some numbers then press the hard enter, that it shifts the focus to the button below the edit text and a QWERTY keyboard appears. How do I stop this from happening?
I have tried to control the enter key with the following:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activityEnterAmt);
final EditText editText = (EditText) findViewById(R.id.editTextEnterAmt);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
Log.i("Tag", "Enter pressed or IME Action Submit pressed");
saveButtonClick(editText);
}
return false;
}
});
}
But I really want to stop it from opening up the keyboard when you press enter. I don't really understand why it is doing this?
Thanks
I have fixed this issue by closing the soft keyboard when the EditText loses focus:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (v == editText) {
if (hasFocus) {
((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(editText,
InputMethodManager.SHOW_FORCED); // open keyboard
} else {
((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
editText.getWindowToken(), 0); // close keyboard
}
}
}
});
Hi I have a problem with my Android app. I use a boolean method to change the function of the back button to make a layout change. As shown in the code if I use a if- statement and if these things are true, the layout changes. The problem is I have more than one point with different situations where I would like to modify the function of the button. But if I copy the function and change the name eclipse wants to remove the # override, and then the method no longer works.
So now the question is: How can I create multiple KeyEvents?
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && lengthisopen == true){
lengthisopen = false;
setContentView(R.layout.length);
return true;
}
return super.onKeyDown(keyCode, event);
}
public boolean onKeyDown2(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && lengthisopen == true){
lengthisopen = false;
setContentView(R.layout.length);
return true;
}
return super.onKeyDown(keyCode, event);
}
onKeyDown(int keyCode, KeyEvent event) is a method available is Activity, So you can override it. But onKeyDown2() is not a super class's method. So you can't override it from any of the super classes. That's why it said you to remove #override.
If you want to create multiple key events then you should go to only
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && lengthisopen == true){
lengthisopen = false;
setContentView(R.layout.length);
return true;
}
else if(condition){
// next code
}
return super.onKeyDown(keyCode, event);
}
I change android soft keyboard to show search icon rather than Enter icon. My question is: how i can detect that user click on that search button?
In the edittext you might have used your input method options to search.
<EditText
android:imeOptions="actionSearch"
android:inputType="text"/>
Now use the Editor listener on the EditText to handle the search event as shown below:
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
// Your piece of code on keyboard search click
return true;
}
return false;
}
});
You can use OnQuerySubmit listener to get keyboard Search button click event.
Here's how,
searchView.setOnQueryTextListener(queryTextListener);
SearchView.OnQueryTextListener queryTextListener
= new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s) {
showLog("searchClickListener onQueryTextSubmit");
getPhotos(searchView.getQuery().toString());
return true;
}
#Override
public boolean onQueryTextChange(String s) {
return false;
}
};