Houssein Zouari
Houssein Zouari

Reputation: 722

keyboard not been shown for edittext

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);
    }

Upvotes: 0

Views: 85

Answers (1)

user4571931
user4571931

Reputation:

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);
}

Upvotes: 1

Related Questions