I have created a trivial application to test the following functionality. When my activity launches, it needs to be launched with the softkeyboard open.
My code doe
If you're trying to show the soft keyboard in a Fragment, you need to wait until the Activity has been created before calling showSoftInput()
. Sample code:
public class SampleFragment extends Fragment {
private InputMethodManager mImm;
private TextView mTextView;
@Override
public void onAttach(Context context) {
super.onAttach(context);
mImm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
showSoftKeyboard(mTextView);
}
/**
* Request the InputMethodManager show the soft keyboard. Call this in {@link #onActivityCreated(Bundle)}.
* @param view the View which would like to receive text input from the soft keyboard
*/
public void showSoftKeyboard(View view) {
if (view.requestFocus()) {
mImm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
}