I am using the OnKeyListener on a EditText on Android to identify when the backspace(delete) of the keyboard was pressed and handle myself the deletion of the character(doing this because it's a field that were synchronized with external service).
Some people said to use the TextWatcher and handle in one of the textchanged events. While this can be a viable solution. I still trying to do it with the onKey.
The problem is that the onKey is only triggered when there is not text(empty) in the EditText and pressed backspace. If there's text the onKey is not triggered until it deletes all text.
Anyone What I am missing for the onKey to be triggered in all the clicks in the backspace?
This is my xml:
<EditText
android:id="#+id/trackpad_speller_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="false"
android:layout_margin="10dp"
android:layout_weight="0.8"
android:focusable="true"
android:focusableInTouchMode="true"
android:imeOptions="actionDone" />
and I am setting the listener on the fragment
this.spellerInput.setOnKeyListener(this);
And my OnKey
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_DOWN) {
if (presenter.getSpellerType() == SpellerType.FREE_SPELLER) {
Log.d(TAG, "backspace pressed and ignored for FREE_SPELLER");
return false;
} else {
Log.d(TAG, "backspace pressed for SEARCH_SPELLER");
presenter.deleteChar();
}
}
return true;
}
Any help would be appreciated! Thanks :D
I don't know if this is the best way, but I found that setting the setInputType of the EditText InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD solves the problem and makes the onKey be triggered in all backspace clicks.
spellerInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
Hope it helps
Related
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?
IDK but i am continuously getting this issue To make request focus on EditText when Keyboard next button is pressed in adapter.
I have set the android:imeOptions="actionNext" in my adapter layout. When i am on first item of 'ListView' then its is working fine.
But when i press next from the keyboard. It goes to next position of EditText in ListView which is perfect but the EditText is not getting the requestFocus() and same problem with other next item.
I tired all the solutions i found out in stackoverflow . But i think i am unable to find out the best solution.
Could you please help me out.
Any help will deeply appriciated. Please check the screenshots below.
May be this will helps you :-
txtUserid.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 Enter key press
txtone.clearFocus();
txtsecond.requestFocus();
return true;
}
return false;
}
});
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"
Hello i have one key listener
email = (EditText)findViewById(R.id.email);
email.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if(keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
return true;
}else{
return false;
}
}
});
but when I press enter the event does not work, any idea?
Check the documentation for the OnKeyListener.
Interface definition for a callback to be invoked when a hardware key
event is dispatched to this view. The callback will be invoked before
the key event is given to the view. This is only useful for hardware
keyboards; a software input method has no obligation to trigger this
listener.
I'm guessing your are not using a hardware keyboard.
Yes i have one solution, im add a android:singleLine="true", regards!
<EditText
android:layout_width="fill_parent"
android:layout_height="52dp"
android:id="#+id/email"
android:background="#drawable/customs_borders"
android:textColor="#ffd3e8d6"
android:gravity="center"
android:textStyle="bold"
android:textSize="23dp"
android:hint="EMAIL"
android:singleLine="true"/>
I need a way to take text from a "EditText" as soon as the user presses "Enter" and it should also call a method if it loses focus.
I created EditText like this:
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/button2"
android:ems="10" >
To receive key presses your EditText needs to have focus:
editText1.setFocusableInTouchMode(true);
editText1.requestFocus();
then set the onkeylistener():
editText1.setonKeyListener(new OnKeyListener(
#override onKey(View v, int keyCode, KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_UP && keyCode==KeyEvent.KEYCODE_ENTER){
// what you need to do when Enter is pressed
return true;
}else return false;
}
For focus lost, you need to implement the View.onFocusChange interface and check that hasFocus is false
Use an EditText action listener. Refer to this question, it's probably the same: Handle Enter in EditText
For detecting loss in focus:
if(!(et.isFocused)){
//call your method here
}