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); } 
5
  • Post your Fragment code Commented Jun 3, 2014 at 7:12
  • 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 comment Commented Jun 3, 2014 at 7:19
  • @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; } Commented Jun 3, 2014 at 7:20
  • @NWD post your all code in your Question. What is this? Commented Jun 3, 2014 at 7:20
  • I used the suggest that is code but i can't post my formated code. So, please teach me how. Great thank. Commented Jun 3, 2014 at 7:21

1 Answer 1

7

Try this way in Fragment

 InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getView().getWindowToken(), 0); 
Sign up to request clarification or add additional context in comments.

2 Comments

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?
@Nin_in_the_winD ya exactly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.