Search code examples
androidviewonclickandroid-softkeyboardandroid-dialogfragment

How do I launch the soft keyboard after "Cancel" button is clicked in DialogFragment?


Fragment.java file:

....
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.skyfrag_layout, container, false);
    getDialog().setTitle("Delete skycard");
    Button btnCancel = (Button) rootView.findViewById(R.id.btnCancel);
    btnCancel.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
          getDialog().cancel();
        }
    });
...

I tried each of the below in the onClick function but neither worked.

1) getDialog().getWindow().setSoftInputMode( LayoutParams.SOFT_INPUT_STATE_VISIBLE);

2)InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);


Solution

  • I guess that you are trying to show the keyboard again because you have a widget that needs editing once your dialog is closed.

    Have you tried calling showSoftInputFromInputMethod (IBinder token, int flags) instead of toggleSoftInputFromWindow ? See the doc here. (Note: the IBinder can be obtained by yourView.getWindowToken())

    Hope this helps.