Question: I have an EditText inside a fragment, which is in itself inside an actionbarsherlock tab. When I touch inside the EditText box a soft keyboard appears with one of the keys having a magnifying glass (search) icon. When I type some text and click on the search key I can process the typed-in-string in my onEditorAction, but the soft keyboard remains on display. How can I close it programatically?

By the way if one answer is that I could configure some setting for EditText such that it closes automatically on search, I would still like to know if the soft keyboard can be closed with a method call as I also have my own search button on screen (nothing to do with the soft keyboard) and I would like the soft keyboard to close when that's pressed too.

Note: Before anyone rushes to claim this question is a repeat of a previous question, I have seen many Q&A's about hiding the soft keyboard at various points. Many of the answers seem inordinately complicated and in many it is not clear whether the idea is to permanently hide the keyboard or just just temporarily close it till the user taps on an EditText field again. Also some answers require calls to methods not available in fragments.

Answer: 
This is working code to hide soft keyboard for android.

```
try {
            InputMethodManager input = (InputMethodManager) activity
                    .getSystemService(Activity.INPUT_METHOD_SERVICE);
            input.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
        }catch(Exception e) {
            e.printStackTrace();
        }

```
A clear way to close keyboard and clearfocus of an EditText inside a fragment, is to make sure that your EditText XML has :

```
android:id="@+id/myEditText"    
android:imeOptions="actionDone"

```

Then set listener to your EditText (with Kotlin, and from a fragment):

```
myEditText.setOnEditorActionListener({ v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                myEditText.clearFocus()
                val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                imm.hideSoftInputFromWindow(view!!.windowToken, 0)    
            }
            false
        })

```