Home » Android » How to display custom keyboard when clicking on edittext in android

How to display custom keyboard when clicking on edittext in android

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have a custom keyboard in my application. question is How to didplay this keyboard when click on the edittext.I an using setonfocuschangre listener ,now the custon keyboaed appears when the edittext focus is changed.but i want to show this keyboard whenever i click on the edittext..one info I forget to put here the edittext is within the fragment.

How to&Answers:

I created a Custom Keyboard in my application using Keyboard tag. I am adding this keyboard in a RelativeLayout on my screen like.

private void createCustomKeyboard() {
  Keyboard customKeyboard = new Keyboard(getActivity(), R.layout.keyboard);
  CustomKeyboard mCustomKeyboard = new CustomKeyboard(getActivity(), this);
  mCustomKeyboard.setKeyboard(customKeyboard);
  RelativeLayout relLayKeyboard.addView(mCustomKeyboard);  
} 

If you want to use this CustomKeyboard on one or more than one EditText then you have to use below code :

EditText edtxtName = (EditText) v.findViewById(R.id.edtName);
RelativeLayout relLayKeyboard = (RelativeLayout)findViewById(R.id.relLay_keyboard);
edtxtName.setOnTouchListener(exitSoftKeyBoard);

private final OnTouchListener exitSoftKeyBoard = new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
    InputMethodManager imm = (InputMethodManager) getActivity().getApplicationContext().getSystemService(
            android.content.Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    if(v.equals(edtxtName)){
        edtxtName.requestFocus();
        relLayKeyboard.setVisibility(View.VISIBLE);
    } 
    return true;
  }
};

Answer:

You can try something like this

    edittext.setOnClickListener(new OnClickListener() {
                    // NOTE By setting the on click listener, we can show the custom keyboard again,
                   // by tapping on an edit box that already had focus (but that had the keyboard hidden).
                    @Override public void onClick(View v) {
                        showCustomKeyboard(v);
                    }
          });


          // Disable standard keyboard hard way
          // NOTE There is also an easy way: 'edittext.setInputType(InputType.TYPE_NULL)' 
         // (but you will not have a cursor, and no 'edittext.setCursorVisible(true)' doesn't work )
                edittext.setOnTouchListener(new OnTouchListener() {
                    @Override public boolean onTouch(View v, MotionEvent event) {
                        EditText edittext = (EditText) v;
                        int inType = edittext.getInputType();       // Backup the input type
                        edittext.setInputType(InputType.TYPE_NULL); // Disable standard keyboard
                        edittext.onTouchEvent(event);               // Call native handler
                        edittext.setInputType(inType);              // Restore input type
                        return true; // Consume touch event
                    }
                });


        // Disable spell check (hex strings look like words to Android)
        edittext.setInputType(edittext.getInputType() | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

For more info check here

Answer:

Use getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); to disable the default keyboard and then set a click listener to show your own keyboard

Answer:

Use onClickListener like following:

edit_text.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View v) {
        custom_keyboard.open();
    }
});

Or you can do this:

edit_text.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus)
                custom_keyboard.open();
            else
                custom_keyboard.close();
        }
    });