Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?!

like image 466
Maniac Avatar asked Sep 04 '14 22:09

Maniac


3 Answers

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);
   }
}
like image 148
bpawlowski Avatar answered Nov 18 '22 23:11

bpawlowski


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!

like image 3
Merlí Escarpenter Pérez Avatar answered Nov 18 '22 23:11

Merlí Escarpenter Pérez


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);
   }
like image 1
Rishabh Srivastava Avatar answered Nov 18 '22 23:11

Rishabh Srivastava