Android Predictive Keyboard
Build your own predictive keyboard using Machine Learning

In a previous post, we saw how to create a machine learning model that generates text. I will be using the results of that project to show how to create your own predictive keyboard in Android.
How does it work?
Predictive keyboards allow to write better and faster by suggesting corrections and possible next words to the user.
There are different ways of creating the magic behind a predictive keyboard: from search in embedded dictionaries to tree structures to more sophisticated machine learning solutions using Natural Languages Processing (NLP).
We will be using a language model (NLP) solution following the previous project NLP with Kotlin.
To train the model we will be using a list of movie titles. This dataset was obtained from IMDb which offers different files containing movie and series information. The file is processed to extract the information needed for this project so that it becomes the training data for the model.
By using a specific dataset, this predictive keyboard reduces its scope. Far from being a handicap this can be seen as having a keyboard that works in a specific domain. If your business is about movies, then this solution is great as the keyboard will only suggest words that are movie-related!
Once the model is trained, a file containing the language model is serialised. This is the file that the Android application will use to generate the suggestions.
Implementation
To implement the keyboard I used the code of SoftKeyboard in AOSP. This is a simple keyboard that looks quite old but that will allow us to focus on integrating the NLP solution.
After adding the generated model (saved_model
) and the library ngram-model-1.0-SNAPSHOT
containing the NLP code, I integrated it with the keyboard itself.
The NLP solution generates a single character with every prediction so I needed to work on the client side to generate words. This is achieved by getting the character predictions from the input text and building words on top of them.

This flow is translated into the following code within the class SmartKeyboard
:
The first time the keyboard is used it can take some time to react while the model is being loaded. Though it’s not the best way it is enough for this PoC. Alternatives to this approach would be to have a database storing the values of the model. I’ll leave this improvement to the reader.
The following gif shows the keyboard in action:

Installation
To install the keyboard in Android go to
Settings -> Languages & Input -> Virtual Keyboard -> Manage Keyboards
and select it.
Source Code
You can find the source code in the following repository in GitHub:
SmartKeyboard for Android
Keyboard using predictive words generated by an NPL Ngram model -
github.com
Feel free to fork it, use it and leave some feedback.
If you want better predictions, use saved_model_multilingual
. This model has been trained with more movie titles. It also takes more time to load.
References
- I highly recommend the article by Adam Sinicki “Let’s build a custom keyboard for Android” to know how to create a custom keyboard in Android