Saturday, May 4, 2019

Android: how to prevent soft keyboard from showing up on activity startup


If the first widget of the activity is an EditText, it will get focus when the activity starts and the soft keyboard will automatically show up. This behavior, however, is not desired sometimes. To hide the soft keyboard in this situation, we can add this code in the onCreate() method of the activity or the onCreateView().

    protected void onCreate(Bundle savedInstanceState) {
... ...      getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

... ...
}

or

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
... ...        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
... ...

}



No comments:

 
Get This <