I have a custom DialogFragment with a single EditText and no buttons. After the text is entered into the edittext and I press "done", the keyboard stays visible upon returning to the host activity, even though the dialog is dismissed. It changes to a numerical keyboard, because the field in the host activity it focuses on is a numerical entry only edittext
- but it may also focus on an edittext field I with normal text entry (and thus remain a normal keyboard) depending on where I left the cursor after I start the dialog fragment.
I have tried everything I could find (have googled and stackoverflowed a lot).
I based the dialogfragment on this code - it is very similar.
The problem is very similar to this, and I think the causes may be similar, but I have no buttons in my DialogFragment so I can't follow that solution though I don't think it matters I can follow it closely enough.
I have tried using inputmethodmanager
as suggested there and here to dismiss the keyboard, within the interface method (implemented in the host activity), and onCreateView
, onEditorAction
and onDismiss
within the DialogFragment class. Also tried getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
(also using SOFT_INPUT_STATE_HIDDEN
) in the DialogFragment class methods. Did try mEditText.clearFocus();
too in all the DF class methods to no avail.
Can anyone help at all? Is it to do with David Chandler's code I based mine on, or something I'm doing wrong specifically. All help most appreciated.
I include my DF class below in case anyone wants to look.
public class SetText extends DialogFragment {
public interface SetTextBoxDialogListener{
void onFinishEnteringName(String name);
}
private EditText mEditText;
//Empty constructor req'd for dialogfragment.
public SetText(){
}
//Build view
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle saveInstanceState) {
View view = inflater.inflate(R.layout.activity_set_the_text, container);
mEditText = (EditText) view.findViewById(R.id.nameText);
getDialog().setTitle("Enter Name");
//removing these 2 lines of code has no effect
mEditText.requestFocus();
getDialog().getWindow().setSoftInputMode(LayoutParams.VISIBLE);
mEditText.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (EditorInfo.IME_ACTION_DONE==actionId){
//return text to activity
SetEnterNameDialogListener activity = (SetEnterNameDialogListener) getActivity();
activity.onFinishEnteringName(mEditText.getText().toString());
dismiss();
return true;
}
//code for 2 lines below had now effect, even placed above dismiss
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
return false;
}
});
return view;
}
}