How to change keyboard view in android after pressing any key - android

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

Related

Android default button and DONE on keyboard

I have an activity with the following structure
- LinearLayout
- EditText (Username)
- EditText (Password)
- Button (Login)
I want that when the user is focused on the second EditText, instead of displaying NEXT in the keyboard, it displays "DONE" or "GO" and by pressing it, the click event of my button is raised.
Is this possible?
Yse, is possible, just use following code:
mUserID.setImeOptions(mUserID.getImeOptions()|EditorInfo.IME_ACTION_NEXT);
mUserPass.setImeOptions(mUserPass.getImeOptions()|EditorInfo.IME_ACTION_DONE);
mUserPass.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView pView, int pActionId, KeyEvent pEvent) {
if (pActionId == EditorInfo.IME_ACTION_DONE) {
// Do something
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.") );
}

Softkeyboard Done button causing focus to skip

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..

combine text(or char) with user input in edittext for android

To All.
Right now i am working on App which launches a dialog(containing EditText) on any alphanumeric key pressed from keyboard. Now i want that whatever user type should appear in EditText box, which is working properly the only issue that i have is, i want the char key which was 1st pressed to open dialog in EditText Box. I am missing the 1st char key which is obviously right. But I retrieve that char as string separately.
Now wanted to attach(show) that char key to inputted EditText from user as 1st letter.
I tried this,
{
final EditText search_d_text = (EditText) dialog
.findViewById(R.id.search_text1);
search_d_text.setText(s1);// si is the 1st char key launch dialog(containing edittext).
search_d_text.setInputType(InputType.TYPE_NULL);
search_d_text
.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (event != null
&& event.getAction() == KeyEvent.ACTION_DOWN) {
// Handle enter key
return true;
}
if (actionId == EditorInfo.IME_ACTION_NEXT) {
// Handle IME NEXT key
return true;
}
if (actionId == EditorInfo.IME_ACTION_DONE) {
// Handle IME DONE key
return true;
}
return false;
}
});
search_string = search_d_text.getText().toString();
enterd_text = search_d_text.getText();
// collecting input text.
}
What i am getting is eg. when i type MASK, It shows ASKM.
The setText(s1) is always act as last letter.
I want to show the word or whatever user input starting from 1st key.
Thank You.
Found solution ,
Just moved edit text cursor position. By
search_d_text.setSelection(s1.length()); i.e to next position.

Hiding the keyboard when user hit the return key on keyboard

First, I don't know what's the key code for the Return key or Backspace the one that has a line on the Android keyboard.
Secondly I have multiple edittext fields on the screen and I want each one to resign the keyboard when the user hit that Return key.
imm= (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
public void onClick(View v) {
int flag;
flag=v.getId();
// keycode for return
if(v.getId()==XX) {
imm.hideSoftInputFromWindow(YYY.getWindowToken(), 0);
}
XX is the keycode for that Return key and YYY is what I should fill in. I would like YYY generic that's applying to all the edittext fields in the program
I'm not sure why you are trying to handle this in an onClick method. The right way, I think, is to call setOnKeyListener() for each EditText view and in your OnKeyListener, you can do this:
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode = KeyEvent.KEYCODE_ENTER) {
// non-null only for enter key
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
return true;
}
return false;
}
A single instance of OnKeyListener can be used for all EditText views (any view at all, actually) where you want this behavior.

Categories

Resources