Search code examples
androidandroid-input-methodandroid-extracted-text

How to modify the ExtractedEditText for a custom keyboard in Android


I want to change the font and text size of the text in a custom keyboard's extracted text view. How do I get a reference to the EditText?

I just learned how to do this by examining the InputMethodService source code, so I am posting this as a Q&A. My answer is below.


Solution

  • You can get a reference to the ExtractedEditText by overriding onCreateExtractTextView() in your InputMethodService subclass. You can modify it there however you like before passing it on.

    @Override
    public View onCreateExtractTextView() {
        View extractedView = super.onCreateExtractTextView();
        ExtractEditText editText = extractedView.findViewById(android.R.id.inputExtractEditText);
        editText.setTypeface(myTypeFace);
        editText.setTextSize(myTextSize);
        return extractedView;
    }
    

    Note that the documentation says:

    public View onCreateExtractTextView ()

    Called by the framework to create the layout for showing extacted (sic) text. Only called when in fullscreen mode. The returned view hierarchy must have an ExtractEditText whose ID is R.id.inputExtractEditText.