According to the documentation , the View (editor) receives commands from the keyboard (IME) through InputConnection and sends commands to the keyboard through InputMethodManager .

I will show all the code below, but here are the steps.
1. Display the keyboard
Since the view sends a command to the keyboard, it needs to use InputMethodManager . For example, we will say that when viewing a view, it will display the keyboard (or hide it if it is already displayed).
@Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY); } return true; }
The view should also have setFocusableInTouchMode(true) .
2. Get keyboard input
In order for the view to receive keyboard input, it needs to override onCreateInputConnection() . This returns the InputConnection used by the Keyboard to communicate with the view.
@Override public InputConnection onCreateInputConnection(EditorInfo outAttrs) { outAttrs.inputType = InputType.TYPE_CLASS_TEXT; return new MyInputConnection(this, true); }
outAttrs indicate which keyboard the view requests. Here we just request a plain text keyboard. Selecting TYPE_CLASS_NUMBER displays a numeric pad (if available). There are many other options. See EditorInfo .
You should return an InputConnection , which is usually a regular subclass of BaseInputConnection . In this subclass, you provide a link to your editable line to which the keyboard will update. Since SpannableStringBuilder implements an Editable interface, we will use this in our base example.
public class MyInputConnection extends BaseInputConnection { private SpannableStringBuilder mEditable; MyInputConnection(View targetView, boolean fullEditor) { super(targetView, fullEditor); MyCustomView customView = (MyCustomView) targetView; mEditable = customView.mText; } @Override public Editable getEditable() { return mEditable; } }
All we have done here is to provide an input connection with a link to a text variable in our user view. BaseInputConnection will take care of modifying this mText . That may be all you need to do. However, you can check the source code and see which methods say "default implementation", especially "default implementation does nothing." These are other methods that you can override depending on how your editor looks. You should also look at all the method names in the documentation . Some of them have notes for "author authors." Pay particular attention to them.
Some keyboards do not send specific input through InputConnection for any reason (for example, delete , enter, and some keys on the numeric keypad ). For them, I added OnKeyListener . Testing this setting on five different keyboards, everything seemed to work. Additional answers related to this are given here:
- Differentiating text code with control code in Android KeyEvent
- Need a table of key codes for android and presenter
- Numeric type keyboard input connection
Full project code
Here is my complete example for reference.

MyCustomView.java
public class MyCustomView extends View { SpannableStringBuilder mText; public MyCustomView(Context context) { this(context, null, 0); } public MyCustomView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { setFocusableInTouchMode(true); mText = new SpannableStringBuilder();
MyInputConnection.java
public class MyInputConnection extends BaseInputConnection { private SpannableStringBuilder mEditable; MyInputConnection(View targetView, boolean fullEditor) { super(targetView, fullEditor); MyCustomView customView = (MyCustomView) targetView; mEditable = customView.mText; } @Override public Editable getEditable() { return mEditable; }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.editorview.MainActivity"> <com.example.editorview.MyCustomView android:id="@+id/myCustomView" android:background="@android:color/holo_blue_bright" android:layout_margin="50dp" android:layout_width="300dp" android:layout_height="150dp" android:layout_centerHorizontal="true" /> </RelativeLayout>
There is nothing special about MainActivity.java code.
Please leave a comment if this does not work for you. I am using this basic solution for a custom EditText in a library that I create, and if there are any edge cases in which it does not work, I want to know. If you want to view this project, a custom view is here . This is InputConnection here .
Similar
- How to create a custom system keyboard
- How to create a custom keyboard in an application