Looking at the Android documentation here:
https://developer.android.com/training/keyboard-input/style.html,
specifically:
EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
sendMessage();
handled = true;
}
return handled;
}
});
This does not close the keyboard after clicking the send button. It also does not close the keyboard if handled is set to false.
What is the appropriate way to hide the keyboard in this case?
Looking at :-
how to hide keyboard after typing in EditText in android?, there seems to be a multitude of ways. Is this really the way to go in Android?
Related
I have edittext when I want to use clipboard or click outside edittext soft keyboard is disappear. How I can fix it any idea
If you return true from your overrided method onEditorAction, system does not going to handle the action again. So, In this case you should return true to not hide keyboard when action is EditorInfo.IME_ACTION_DONE.
Here use this code:
edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
}
return true;
}
});
When I'm writing in an EditText, if I push the next/done key then the focus will move to the next EditText. How to forbid this behaviour (programmatically)?
Just setFocusable(true) to the mother view of your edittext
You may try this code :
yourEditText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT) {
// do something with next printed on your editText
}
return true;
}
});
You can manage cases for different option like other action(Done/Next/Search etc.) too.
I am searching for a way to customize (or handle) the 'Enter' of the virtual keyboard when some app user is giving some input in an Android app, for example, when he is pressing "Enter" in an Android app (in some EditText) or when he has completed giving his input, then at that point (or before) can I customize the 'Enter' of the virtual keyboard (like in some apps' virtual keyboard, it is "Done") to text like "Go" etc. Also, if something like some (other) action could also be possibly performed when that virtual keyboard button (like Enter) is being clicked or pressed. Any kind of help would be greatly appreciated.
You can set different imeOptions like actionDone and actionGo reference link
imeOptions can setted like this in editText
android:imeOptions="actionDone"
to customize these imeActions firstly you can change imeOptions label like this
android:imeActionLabel="#string/anyString"
You have to implements OnEditorActionListener interface and set it to your EditText
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
Toast.makeText(getApplicationContext(), "Your changes have been saved", Toast.LENGTH_SHORT).show();
}
}
});
set ImeOptions
et_input.setImeOptions(EditorInfo.IME_ACTION_DONE);
Editor Action Listener
et_input.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
int result = actionId & EditorInfo.IME_MASK_ACTION;
switch (result) {
case EditorInfo.IME_ACTION_DONE:
break;
case EditorInfo.IME_ACTION_NEXT:
//
break;
}
return true;
}
});
I saw many searchboxes in apps that if click the keyboard layout changes into Search button. How to change the android keyboard layout from Enter to Search if an EditText is click? Just like other search boxes. Thanks
add
android:imeOptions="actionSearch"
to your EditText in your layout file.
After that you can manage "search" click event by following code:
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
performSearch();
return true;
}
return false;
}
});
I have an activity with two EditTexts. If, in landscape mode, I select the first EditText, the keyboard that is displayed has a "next" button which should allow me to type in the second EditText. Likewise, the second EditText has a "done" button, which I would like to handle for completing the activity. How can I handle the selection of these buttons?
There is fairly little documentation on how to solve this issue. I found a good solution here. Basically, you can add an IMEoption for each of the EditTexts:
For the first one:
android:imeOptions="actionNext"
For the second one:
android:imeOptions="actionDone"
To handle these in the code, try something like this:
EditText departureAddress, destinationAddress;
departureAddress = (EditText)findViewById(R.id.departure);
//Set the action of the "next" button to bring destinationAddress to focus
destinationAddress(new OnEditorActionListener(){
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT) {
destinationAddress.requestFocus();
}
return true;
}
});
destinationAddress = (EditText)findViewById(R.id.destination);
//Set the action of the "done" button to handle the map query
destinationAddress.setOnEditorActionListener(new OnEditorActionListener(){
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
//Handle map query
}
return true;
}
});