Softkeyboard Done button causing focus to skip - android

I have a set of EditTexts in my app that the user needs to fill in. Pressing "Next" on the soft keyboard advances the focus to the next EditText and at the last one I capture the soft key Enter action to validate and submit the entered data. In case there is a validation error (e.g. an unfilled field) the relevant field (i.e. the unfilled field) requests focus whilst also showing a toast to display the error.
It works fine (focuses on the correct item) if I use a Send button which calls the same validation function, but if I hit the Enter button on the soft keyboard, the focus jumps to the EditText that caused the validation error and immediately moves to the one immediately after it.
This is the snippet that sets the listener on the last EditText.
lastEditText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
if(validateFields()){ //Validation function
sendPressed(); //Submit function
}
return true;
}
return false;
}
});
Any help would be appreciated..

Related

Edittext not getting focus in listview adapter

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;
}
});

Simulating a click on a Next/Done button in Robolectric

In a given screen i have few controls where the first one is EditText and than RadioGroup and than a Button, at EditText i have used android:imeOptions="actionNext", now for this writing a straightforward Robolectric test case is not a big deal as
we can write
Button someButton = (Button) findViewById(R.id.some_button);
someButton.performClick();
but the question is how to automate and write a test case on clicking Next/Done button of the given softinput KeyBoard. How we can write and perform next/done Button of softinput keyboard click ?
Please guide!!
You've got probably something like this which you want to test:
getView(R.id.etPassword).setOnEditorActionListener(
new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT) {
checkCredentialsAndStartLoginTask();
}
return false;
}
});
This listener is called when you click on "Next" on the soft keyboard. If you want to test this simply call the onEditorAction with the adequate actionId as parameter on the EditText to simulate this soft keyboard button click. See this sample.
#Test
public void emptyUsernameShouldTriggerToast() throws Exception {
activity = controller.create().start().resume().visible().get();
// enter username and password
ui.etAccount.setText("");
ui.etPassword.setText("Battery");
// test initial state
ui.etPassword.onEditorAction(EditorInfo.IME_ACTION_NEXT);
// test Toast
ShadowHandler.idleMainLooper();
assertThat( ShadowToast.getTextOfLatestToast() ).isEqualTo(("Please enter username and password.") );
}

How to change keyboard view in android after pressing any key

I have an Edittext field and want the user to be able to enter the text very quickly. The text that will be entered begins with a capital letter followed by a set of numbers only. So after entering the first letter I want the keyboard view to switch to show numbers only. Here is my code for the edittext listener:
final EditText carPlate=(EditText) findViewById(R.id.carPlateNumber);
carPlate.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(carPlateCharnum==0){
carPlate.setInputType(InputType.TYPE_CLASS_NUMBER);
carPlateCharnum++;
return true;
}
// if keydown and "enter" is pressed
if ((event.getAction()==KeyEvent.ACTION_DOWN)
&& (keyCode==KeyEvent.KEYCODE_ENTER)) {
// display a floating message
DisplayToast(carPlate.getText().toString() + carPlateCharnum);
carPlateCharnum++;
return true;
}
return false;
}
});
The problem is the keyboard view is only switching after I press the enter key. So I need to know where and how to put this part of code:
if(carPlateCharnum==0){
carPlate.setInputType(InputType.TYPE_CLASS_NUMBER);
carPlateCharnum++;
return true;
}
Also I don't want the insertion point to return to the beginning of the EditText field after entering the first letter.
The keyboard has to reinitialize itself to be informed about the changes in the EditText carPlate.
(Detail-Info: TextView.onCreateInputConnection() returns the new InputType to the InputMethodManager).
carPlate.setInputType(InputType.TYPE_CLASS_NUMBER);
((InputMethodManager) YourActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE))
.restartInput(carPlate);

Android: how to ignore Enter key so the focus won't change

I have an Activity with two EditText fields. So when I press enter in the first one the second one becomes focused.
However I want to disable Enter key in the first EditText sometimes (for example when I think that the user haven't made a proper input into the first EditText).
I' ve overwriten onKeyDown for the 1st EditText returning true when key event is KEYCODE_ENTER but that doesn't help.
What shall I do?
How about adding an OnFocusChangeListener? When your EditText looses focus, you can do validation and can do a requestFocus for the "right" field.
Just by adding an editorActionListener:
editText1.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE || event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
if(isValidContent(editText1.getText().toString()){
editText2.requestFocus();
return true;
} else {
....
}
}
return false;
}
});

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