0

I have a select box and edit text in a view.If you perform a longClick on customNumberPicker.It will hide the customNumberPicker and display the Edittext.(The same thing is done for edit text)It is working fine.

But I would like also that the keyboard will be open when switching to the edit text mode.

Here is my code

final NumberPicker numberPicker = (NumberPicker) dialog.findViewById(R.id.npWeight);
    final EditText editText = (EditText) dialog.findViewById(R.id.edWeight);

    numberPicker.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            if (numberPicker.getVisibility() == View.VISIBLE) {
                editText.setVisibility(View.VISIBLE);
                numberPicker.setVisibility(View.GONE);
                editText.setFocusableInTouchMode(true);
                editText.requestFocus();
                showKeyboard(MyApplication.mainActivity);
            }

            return true;
        }
    });

    editText.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
                editText.setVisibility(View.GONE);
                numberPicker.setVisibility(View.VISIBLE);
         //       hideKeyboard(MyApplication.mainActivity);
            return true;
        }
    });


private static void showKeyboard(Activity activity) {
        View view = activity.getCurrentFocus();
        InputMethodManager methodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        assert methodManager != null && view != null;
        methodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
    }
Hardik Talaviya
  • 1,396
  • 5
  • 18
Houssein Zouari
  • 712
  • 6
  • 18
  • 2
    Possible duplicate of [How to show soft-keyboard when edittext is focused](https://stackoverflow.com/questions/5105354/how-to-show-soft-keyboard-when-edittext-is-focused) – Chris623 Mar 14 '19 at 12:30

1 Answers1

1

Try to show keyboard with below options:

InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);                   

inputMethodManager.toggleSoftInputFromWindow(view.getApplicationWindowToken(),InputMethodManager.SHOW_FORCED, 0);

or

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

For fragment try below code :

public void showKeybarod(){
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            if(getActivity() != null && !getActivity().isFinishing() && getUserVisibleHint()) {
                InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
            }
        }
    },300);
}