Maniac
Maniac

Reputation: 774

Show / hide Android softkeyboard on fragment replace

I have an Activity with a fragment. Let's say a list fragment with a list of things. Now I want to let the user add a thing, so I use the FragmentManager to replace the list fragment with an insert fragment which has an EditText. The EditText has the focus and the cursor is blinking. But the softkeyboard doesn't open. Same thing other way round: if the user has entered the new thing and added it to the list, I replace the insert fragment back with a list fragment. But although there is no EditText anymore, the keyboard doesn't close.

What is the correct way to implement this? I can't believe that I have to show and hide the keyboard manually on all transitions?!

Upvotes: 26

Views: 16305

Answers (9)

ftm.amani
ftm.amani

Reputation: 81

for Kotlin :

  1. put this function in somewhere like Utils class.
fun hideSoftKeyboard(view: View, context: Context) {
        val inputMethodManager = 
            context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
    }

then use it in Fragments or Activities Like:


Utils().hideSoftKeyboard(inputTextChat, requireContext())

Upvotes: -1

I have the same problem with my app and finally we have 2 options, at first create a general function and call this in all transitions or create a global transition how this:

public static void RemoveAndReplaceFragment(FragmentManager fragmentManager, int FragmentContent, Fragment PlaceHolderFragment,Activity context){
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.remove(fragmentManager.findFragmentById(R.id.frgMainActivity))
                .commit();

        //Close keyBoard in transition
        InputMethodManager inputManager = (InputMethodManager) context.getSystemService(
                Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(),
                InputMethodManager.HIDE_NOT_ALWAYS);

        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.setCustomAnimations(R.anim.animation_fade_in,R.anim.animation_fade_out);
        fragmentTransaction.replace(R.id.frgMainActivity, new PlaceholderFragment_MainActivity_AcceptAndFollowTask()).commit();
    }
}

The best way is capture the transition event but we can't in this moment... Tell me if I helps you, good luck!

Upvotes: 3

Rishabh Srivastava
Rishabh Srivastava

Reputation: 3745

This will work for sure:

private void hideKeyboard() {   
    // Check if no view has focus:
    View view = this.getCurrentFocus();
    if (view != null) {
        InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }

    private void showKeyboard(){
    EditText myEditText = (EditText) findViewById(R.id.myEditText);  
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
   }

Upvotes: 1

bpawlowski
bpawlowski

Reputation: 1046

I would to following things:

1. Extend Fragment class
2. Override onAttach() and onDetach() callbacks
3. Implement show and hide software keyboard method

sample code:

class MyFragment extends Fragment {
   @Override
   public void onAttach(Activity activity) {
       super.onAttach(activity);

       //show keyboard when any fragment of this class has been attached
       showSoftwareKeyboard(true);
   }

   @Override
   public void onDetach() {
       super.onDetach();

       //hide keyboard when any fragment of this class has been detached
       showSoftwareKeyboard(false);
   }

   protected void showSoftwareKeyboard(boolean showKeyboard){
       final Activity activity = getActivity();
       final InputMethodManager inputManager = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);

       inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), showKeyboard ? InputMethodManager.SHOW_FORCED : InputMethodManager.HIDE_NOT_ALWAYS);
   }
}

Upvotes: 12

Durgesh
Durgesh

Reputation: 291

you can use below code to hide soft keyboard.

InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);

Upvotes: 0

Eugene H
Eugene H

Reputation: 3568

I agree, I couldn't believe that you had to manually hide the keyboard as well.

Add this to your Fragment onCreate that you do not want the Keyboard not to be in:

   ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(mToolbar.getWindowToken(), 0);

change mToolbar to any view within the layout. In this case I used my toolbar.

Add this to the Fragment onCreate that you want the keyboard to be shown.

        ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

Upvotes: 0

gunjan luthra
gunjan luthra

Reputation: 121

Try this

InputMethodManager imgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

    imgr.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    editText.requestFocus();

Upvotes: 0

usethe4ce
usethe4ce

Reputation: 23819

One way is to call Activity.recreate, since it is necessary to handle the orientation-change case anyway. Seems like overkill, and you also end up with different transition animations.

Upvotes: -1

Bojan Kseneman
Bojan Kseneman

Reputation: 15668

I am guessing that your ListView is stealing the focus of your EditText, try this and let me know if it helps.

getListView().setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);

Upvotes: 0

Related Questions