Close/hide the Android Soft Keyboard on activity state onStop
Asked Answered
I

1

9

I have an EditText and a Button in my layout. After writing in the edit field and click this button to go back my fragment, I want to hide the virtual keyboard. I assume that there's a simple, but i tried some way and it not work:

That code show how the Button work:

private void onButtonClicked(){
    getActivity().getSupportFragmentManager().popBackStack();
}

That code for some solution but that can't help. This code i using hideSoftInputFromWindow but when i call 'EditText.getWindowToken()', it not hide the soft keyboard (I also change the 0 value to InputMethodManager.HIDE_IMPLICIT_ONLY or InputMethodManager.HIDE_NOT_ALWAYS and it not work):

EditText myEditText = (EditText) findViewById(R.id.myEditText);  
InputMethodManager imm = (InputMethodManager)getSystemService(
      Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

With this code, in another screen of this app, it work. This screen is an activity so i think that problem is fragment's problem.

My fragment code:

public class ChangeEmailFragment extends BaseFragment {
    private TextView mTxtCurrentEmail;
    private EditText mEdtNewEmail;
    private EditText mEdtPassword;
    private TextView mTxtSubmit;

    @Override
    public void onStop() {
        super.onStop();
        if (progressDialog != null && progressDialog.isShowing())
            progressDialog.dismiss();
        if (dialog != null && dialog.isShowing())
            dialog.dismiss();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_change_email, container,
                false);
        mTxtCurrentEmail = (TextView) view.findViewById(R.id.current_email);
        mEdtNewEmail = (EditText) view.findViewById(R.id.edit_email);
        mEdtPassword = (EditText) view.findViewById(R.id.edit_password);
        mTxtSubmit = (TextView) view.findViewById(R.id.button_submmit);
        return view;
    }

    private void showErrorDialog(String msg) {
        Builder builder = new Builder(getActivity());
        builder.setTitle(getString(R.string.fg_change_email_dialog_error_title));
        builder.setMessage(msg);
        builder.setNegativeButton(getText(R.string.common_ok), null);
        dialog = builder.create();
        dialog.show();
    }
}

My activity code:

@Override
    public void onStop() {
        super.onStop();
        InputMethodManager imm = (InputMethodManager) getSystemService(
              Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(mEdtUserName.getWindowToken(), 0);
    }
Inclement answered 3/6, 2014 at 7:7 Comment(5)
Post your Fragment codeProbabilism
That is my fragment code public static ChangeEmailFragment newInstance() { ChangeEmailFragment changeEmailFragment = new ChangeEmailFragment(); return changeEmailFragment; } Because it too long so i will post that at the next commentCamise
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_change_email, container, false); mTxtCurrentEmail = (TextView) view.findViewById(R.id.current_email); mEdtNewEmail = (EditText) view.findViewById(R.id.edit_email); mEdtPassword = (EditText) view.findViewById(R.id.edit_password); mTxtSubmit = (TextView) view.findViewById(R.id.button_submmit); return view; }Camise
@NWD post your all code in your Question. What is this?Probabilism
I used the suggest that is code but i can't post my formated code. So, please teach me how. Great thank.Camise
P
7

Try this way in Fragment

 InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
 imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
Probabilism answered 3/6, 2014 at 7:10 Comment(2)
That work for me. Thank you! So i understand that when my cursor place at fragment, i must call hideSoftInputFromWindow in my fragment but not activity. Is that right?Camise
@Nin_in_the_winD ya exactly.Probabilism

© 2022 - 2024 — McMap. All rights reserved.