Mick
Mick

Reputation: 7927

How to close soft keyboard in fragment

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.

Upvotes: 9

Views: 19336

Answers (6)

CAZ
CAZ

Reputation: 119

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

Upvotes: 0

Keshav Gera
Keshav Gera

Reputation: 11244

Working in Fragment

    getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

Upvotes: -1

Shajeel Afzal
Shajeel Afzal

Reputation: 5953

You can check my answer here. It was the only way that worked for me inside fragment.

Upvotes: 0

rciovati
rciovati

Reputation: 28063

In my fragments I close the keyboard simply in this way:

public static void closeKeyboard(Context c, IBinder windowToken) {
    InputMethodManager mgr = (InputMethodManager) c.getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.hideSoftInputFromWindow(windowToken, 0);
}

closeKeyboard(getActivity(), yourEditText.getWindowToken());

Upvotes: 39

Muhammad Aamir Ali
Muhammad Aamir Ali

Reputation: 21087

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

Upvotes: 7

zozelfelfo
zozelfelfo

Reputation: 3776

I'm using this code in a fragment

InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(
                            Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(text.getWindowToken(), 0);

when I click on an action bar icon and it's working, I don't see why it shouldn't work in your case (maybe I misunderstood the question).

Upvotes: 2

Related Questions