androidvirtual-keyboardandroid-virtual-keyboard

Hiding the virtual keyboard but have a fully functional cursor


I'm currently developing a calculator app where I have made a custom keypad and would like to hide the virtual keyboard. I have found solutions where I can hide it, but the cursor also gets hidden. The functionality I want is the same as the com.android.calculator2 app. I have looked at the source code of that but I still can't get it to work.


Solution

  • I think you are getting it wrong. There is a much easier solution(and a more obvious one).

    1. Make the EditText uneditable.
    2. Bind to the EditText in your code (findViewById)
    3. In your buttons, get the text and add to the current string and then display it.

    Eg.

    say you pressed the '1' button.

    in your one.setOnclickListener(), do this:

    String S=EditText.getText()+"1"; 
    EditText.setText(s);
    

    Edit:

    If you just want to hide the keyboard while keeping the cursor, try this code:

    EditText editText = (EditText)findViewById(R.id.edit_text);
    editText.setOnTouchListener(new OnTouchListener() {
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            v.onTouchEvent(event);
            InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }                
            return true;
        }
    });