Android edittext soft keyboard disappear when click outside edittext - android

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

Related

What's the appropriate way of handling IME_ACTION_SEND in EditText?

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?

Android Keyboard Layout Search

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

Android - How to hide keyboard when search button of the keyboard is pressed?

How to hide keyboard when search button of the keyboard is pressed?
When the user finishes inputing in edittext and press 'search button' fo the keyboard, the keyboard should be hidden..
Set imeOptions to "actionSearch" in your xlm for edit text
Initialize listeners for your EditText
searchEditText.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;
}
});
Hide keyboard when user clicks search.
private void performSearch() {
searchEditText.clearFocus();
InputMethodManager in = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);
... perform search ...
}

How to lose the focus of a edittext when "done" button in the soft keyboard is pressed?

I have 7 edittext boxes in my xml. I'm using the OnFocusChangeListener to read the value from edittext and i'm using that value for my calculation.I want to make my edittext to lose its focus when i click on the done button in the soft keyboard.so that i can get the value in the edittext.
Call clearFocus method of EditText to lose focus when done button is clicked from soft-keyboard. do it as:
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
//Clear focus here from edittext
editText.clearFocus();
}
return false;
}
});
and also add android:imeOptions="actionDone" in edittext xml
If anyone comes across this question wondering how they could unfocus everything at once in their activity, they can set the focus on the parent layout (or any layout for that matter).
findViewById(R.id.myLayout).requestFocus();
Just a little more complete answer
// This will change the ‘RETURN’ button in your the EditText’s soft keyboard to a ‘DONE’ button.
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
// Use the InputMethodManager to close the soft keyboard
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
//Clear focus here from edittext
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
return false;
}
});
Kotlin, it worked for me:
main.clearFocus()
main is a root ConstraintLayout
Kotlin version of ρяσѕρєя K's answer:
editText.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
editText.clearFocus()
}
false
}

How can I use the android landscape keyboard buttons?

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

Categories

Resources