Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close/hide the Android Soft Keyboard on activity state onStop

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);
    }
like image 343
Trần Đức Tâm Avatar asked Jun 03 '14 07:06

Trần Đức Tâm


1 Answers

Try this way in Fragment

 InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
 imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
like image 158
M D Avatar answered Nov 13 '22 07:11

M D