ImeAction Search is changed to Done after tapping on it - android

I have an Edittext for searching on Listview.
I have set imeOption to 'IME_ACTION_SEARCH' so that soft-keyboard will show search key on it.
The problem is that when I tap on search key on keyboard if edittext contains no text in it, the search key changes to 'Done' instead of dismissing the keyboard.
If the Edittext contains some text in it the search key works well.

In your XML layout file you can set
<EditText android:imeOptions="actionSearch" />
Or in your Java source file you could do
yourTextField.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
Then you can override the listener for the search event.
yourTextField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
//if textfield value is empty then close keyboard.
//else call your search function.
}
}
});

you should do this to your edittext in xml
android:imeOptions="actionSearch|actionSearch"
this will show you search button always!

I found the problem behind this. It was happening because the OnEditorAction Listener was not properly set on Edittext. Since listener was not properly set call was not reaching onEditorAction() method.

For my case, I fixed it by -
myEditText.setOnEditorActionListener((v, actionId, event) -> {
/* First check that if the edit text is empty or not */
if (!TextUtils.isEmpty(myEditText.getText().toString()) && actionId == EditorInfo.IME_ACTION_SEARCH) {
/* Do your task here */
}
return true; /* Must return true because here we customize the action */
});
According to this official documentation,
Return true if you have consumed the action, else false.
So we need to return true here because we customize the action here and it will prevent change. Also, be ensured that your EditText is containing the flag -
android:imeOptions="actionSearch"

Related

onEditor action not working when trying to respond to the tick on the android numeric soft keyboard

I have an EditText in my app with an input type of number:
<EditText
android:id="#+id/answerText"
style="#style/GeneralTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/question"
android:layout_toRightOf="#id/equals"
android:inputType="number"
android:paddingTop="#dimen/activity_vertical_margin"
android:maxLength="3"
android:visibility="invisible"
tools:text="ans"/>
So when the EditText has focus the numeric keyboard is displayed. I am trying to repond to the user pressing the tick button after supplying a value in the EditText but I can't get it to work.
I've tried the following code which I've put inside the onCreate method but it isn't working:
answerGivenText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE){
return true;
}
return false;
}
});
Can anyone help please?
I think you should add the
android:imeOptions="actionDone"
attribute to your edittext.
That code is exactly what you need, but you are not doing anything inside it, you're just intercepting the click and returning true, indicating that you handled it to other listeners on the stack.
You would do whatever it is you want to do when the user clicks the tick in the
if (actionId == EditorInfo.IME_ACTION_DONE)
section. And turn on the DONE action in your layout. What exactly are you trying to achieve?

Android: How do I stop my edit text from going to next one?

so i have a login form. When i press the back button or the enter button, it seems to go the next edit text (or previous) even though i set these following options for my edit text.
What is weird, once i cycle through them all, everything works fine as it should i.e. back button / done button dismiss the edit text and key board.
Please note i am only showing 1 edit text field to not flood the post. The rest are pretty much the same
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<com.test.views.widgets.myEditText
android:id="#+id/userFirstName"
android:imeOptions="actionDone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/generic.first.name"
android:inputType="textPersonName"
android:textColor="#android:color/white"
android:theme="#style/EditText.Base.Style"
app:font="regular" />
</android.support.design.widget.TextInputLayout>
So the above, i have set actionDone which was one of the suggestions to this problem. This does not work.
In addition, I am extending the AppCompatEditText to have my own implementation.
Here is some code
When done button is pressed
#Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if(actionId == EditorInfo.IME_ACTION_DONE){
clearFocus();
}
return false;
}
When the back button is pressed
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
clearFocus();
}
return false;
}
Also note that i followed this tutorial as a means to hide keyboard when focus changes
http://toastdroid.com/2015/06/04/on-screen-keyboard-tracking-part-2/
Add android:imeOptions="actionDone". It will close keyboard rather than going on next focusable view.
add this to the EditText attribute
android:maxLines="1"
Try adding android:imeOptions="actionDone" to your EditText
OR
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.hideSoftInputFromWindow(edittext.getWindowToken(), 0);
return true;
}
return false;
}
});
I suppose that problem is in your clearFocus(). Documentation says:
Note: When a View clears focus the framework is trying to give focus
to the first focusable View from the top. Hence, if this View is the
first from the top that can take focus, then all callbacks related to
clearing focus will be invoked after which the framework will give
focus to this view.
try to replace clearFocus() with hideKeyboard() like this
void hideKeyboard() {
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(yourView.getWindowToken(), 0);
}
I managed to fix this.
I added this to my root layout (which contained the edit text) and the problem no longer seems present
android:focusableInTouchMode="true"

Android: Remove Enter Key from softkeyboard

In my login form when user clicks on an EditText and presses the enter key, this inserts a new line, therefore increasing the EditText's size. Next moment, it returns to its previous place and prints a dot in the password field (which is the next field).
I want to remove this enter key from the softkeyboard. Is it possible?
Use :
android:singleLine = "true"
or
edittext.setSingleLine();
And your ENTER key is gone
add this tag to textView in xml
android:singleLine = "true"
I am afraid you can't do this. But one thing is you can handle the softkeyboard keyevents like this,
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
Log.i("event", "captured");
return false;
}
else if(event.getAction() == KeyEvent.ACTION_DOWN
&& event.getKeyCode() == KeyEvent.KEYCODE_BACK){
Log.i("Back event Trigered","Back event");
}
}
}
return false;
}
});
Apart from this, you have to note that providing the attribute android:singleLine=true will make your edittext from growing in size when the soft keyborad ENTER is pressed
Inside the tag EditText you only have to do:
android:singleLine="true"
this remove the enter key in the keyboard
UPDATE
Inasmuch as android:singleLine="true" is deprecated I use android:maxLines="1" to avoid the enter in a EditText. How the name of the method says only N lines is permitted.
New update :
android:maxLines="1"
If you want something more generic in your .java:
boolean state = true;
yourTextInputEditText.setSingleLine(state);

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.

android virtual keyboard listener

Though i have asked the question before but dint get any proper answer. I have an EditText when edit text is focused android virtual keyboard pops up, i have added Done button to the keyboard using ime option from property window. Now i want to perform some action by pressing the Done button. How to do this? Please any body help.
you an do it by firest finding reference of edittext and than set belows code for ur edittext:
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
performSearch();//do here your stuff f
return true;
}
return false;
}
});
here i have taken editText name which is ref. editextbox which u added and you have to add actionId==type of ime options which u have setted

Categories

Resources