Search code examples
androidandroid-softkeyboardandroid-dialogfragment

Android - Dialog fragment: always hide virtual keyboard


I have a custom dialog which is a DialogFragment. This dialog have a EditText and my own keyboard view so I don't want to use the default virtual keyboard. I hide the virtual keyboard everytime user touch the EditText:

edtAmount.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        v.onTouchEvent(event);

        View view = this.getDialog().getCurrentFocus();
        if (view != null) {
            InputMethodManager imm = (InputMethodManager) getActivity()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(),0);

        }
        return true;
    }
});

But because the system still call the virtual keyboard to show (Before is force to hide it), then system move my dialog up and down very quickly. This is not good.

Can someone help me to avoid the dialog pushed up like this, just keep it stay still?

PS: I tried in Manifest:

android:windowSoftInputMode="adjustNothing" 

But seem like not work.

Thank you very much.

EDIT I want to keep the cursor so I find the solution in this thread: https://stackoverflow.com/a/14184958/2961402

Hope this help some one.


Solution

  • This can only done when you extends the EditText from your custom EditText, please use the below code for custom EditText which never open Soft Keyboard ever...!

    public class DisableSoftKeyBoardEditText extends EditText {
      public DisableSoftKeyBoardEditText(Context context, AttributeSet attrs) { 
      super(context, attrs);     
      }      
      @Override      
      public boolean onCheckIsTextEditor() {   
      return false;     
      }        
      }