Affiliate links on Android Authority may earn us a commission.Learn more.

Let’s build a custom keyboard for Android

July 31, 2025

When thinking ofbuilding an Android app, we often think of something with a screen and a contained function. It could be a game, or a tool to perform a common task.

But apps can come in a variety of shapes and sizes. You could build a service that runs in the background and quietly makes life easier for the user. You could create a widget, or a launcher. How about a keyboard?

Article image

Upgrading the software keyboard on your device is one of the most profound ways to customize a device. Most of us use the keyboard as our primary input method. It’s integral to nearly every interaction with your phone. In the best-case scenario, it can make everything quicker, easier, and less error-prone.

Keyboard apps can be highly successful for this reason too; just look at the ubiquity of Swype and SwiftKey.

Article image

Whether you just want to take your Android customization to the next level, or you’d like to sell a whole new way to interact with a smart device, read on and let’s explore how to create an Android keyboard.

Note:This project is relatively simple and requires mostly copying and pating XML script. However, it does include some more advanced concepts like services and inheritance. If you’re happy to follow along to get a keyboard running, then anyone should be able to reproduce the app. If you want to understand what everything does, this is a good intermediate project to wrap your head around. You will of course needAndroid Studio and the Android SDK already set-up.

Article image

Layout files. LOTS of layout files

To build our custom keyboard, we’re first going to need to create a new xml file, which will define the layout and appearance of our keyboard. That file will be calledkeyboard_view.xml. To create this, right click on the “layout” folder in your “res” directory and choose “layout resource file.” In the dialog box that pops up, clear the text where it says “Root element” and start typing “keyboard.” Select the first option which comes up, which should be:android.inputmethodservice.KeyboardView. Call the filekeyboard_view.xml(remember, no capitals for resources!).

You will be greeted by a file looking like this:

We’re going to add a few elements now:

We’ve assigned an ID here so we can refer to the keyboard later in our code. The code aligns our keyboard to the bottom of the screen and the background color is set tocolorPrimary.This color is the one set in ourvalues > colors.xmlfile — it’s easy to change later on. So just hop in there and change the respective color code to change the look a little.

We’ve also referred to another layout for “keyboard preview.” In case you’re scratching your head, that’s the image of the key that flashes up in a large font when you make contact. This assures the user they hit the right key.

Article image

As you’ve possibly guessed, this means we need another new layout file, the aforementionedkeyboard_preview.xml. Create it in just the same way, though the root element this time isTextView.

Add this code and you’ll define the color of the square and the color of the letter appearing in the square. I also set the alignment to center, which ensures it looks the way it should.

Article image

The next new XML file is calledmethod.xml. This will go in your resources folder and have the root elementinput-method. This file will tell Android what type of input is available through your app. Again, you want to replace the boilerplate code that’s there so that it reads like this:

You can also put information such as language in here later on.

This is where we will create the layout for our keyboard — it’s nearly the fun part!

That will go in a new directory you’ll create (res — xml) and I’m calling minekeys_layout.xml. Replace the code that’s there with this:

This is what we’ll be populating with the keys and their behaviors.

Designing your keyboard

We’ve built a bunch of XML files and now we’re ready to start having some fun. It’s time to create the layout of the keys!

This is what I used. It’s basically a slightly tweaked version of a keyboard layout I found online, with the keys all in standard rows. It’s not exactly beautiful, but it’ll do.

You’ll notice a few interesting things here. Theandroid:codestell us what each key needs to do. This is what we’ll be receiving through our service shortly and you need to ensure thekeyLabel(the text on the keys) lines up with what it actually does. Well, unless your objective is to create a “troll keyboard.”

If you place more than one code separated by commas, your keys will scroll through those options if the user double or triple taps. That way we can make a keyboard that works like the old T9 numpad keyboards on Nokia phones, for instance.

Negative codes represent the constants in the keyboard class. -5 is the equivalent ofKEYCODE_DELETE. Play around, use your imagination, and see if you can come up with a “better keyboard.”

An obvious choice is to make the more popular keys a little larger. That’s what I’ve started doing.

At your service

Now it’s time to create a java class. This is going to be calledMyInputMethodServiceand, as the name suggests, it’s going to be a service. The superclass will beandroid.inputmethodservice, meaning it will inherit properties from that kind of class and behave like an input method service should (politely).

UnderInterface(s), we’ll be implementingOnKeyboardActionListener. Start typing and then select the suggestion that springs up.

Being a service, this means your app can run in the background and then listen out for the moment when it is needed – when the user selects an edit text in another application for example.

Your class will be underlined red when this is generated, which is because it needs to implement the methods fromInputMethodService. You can generate this automatically by right clicking on your class and choosinggenerate — implement methods.

Here’s what it should look like:

You also need to override theonCreateInputView()method, which is going to grab the keyboard view and add the layout onto it.

Now add the following code, remembering to import all classes as necessary.

When the input view is created, it takes the layout filekeyboard_viewand uses it to define how it looks. It also adds thekeys_layoutfile we created and returns the view for the system to use.

I’ve also added a Boolean (true or false variable) calledcapsso we can keep track of caps-lock.

The other important method here, is the one handling key presses. Try this:

This is a switch statement which looks for the key code and acts accordingly. When the user clicks specific keys, the code will change course.KEYCODE_SHIFTchanges ourcapsBoolean, sets the keyboard to “Shifted,” and then invalidates the keys (to redraw them).

commitTextsimply sends text (which can include multiple characters) to the input field.sendKeyEventwill send events like “return” to the app.

The class in its entirety should look like this:

Testing it out and customization

In order to test your new keyboard, you’ll need to add it via your device’s settings. To do this, go toLanguage & Input — Virtual Keyboard — Manage Keyboardsand turn on the keyboard you created. Select “OK” a few times to dismiss the notifications.

Now open up any app with a text input and bring up your keyboard. You’ll notice a little keyboard icon in the bottom right. Select that and then pick your app from the list. If all has gone to plan, your keyboard should now spring to life!

This is a little confusing for new users, so if you plan on selling this app, it might be a good idea to add some text to theMainActivity.Javafile, explaining how to select the keyboard. You could also use this to add some customization or settings for the users to tweak.

You could add plenty of customization options. How about letting the user change the height and size of their keyboard? You could let them change the colors, use different icons for the keys (android:keyicon), or change the images entirely (android:keybackground=@drawable/). For more advanced options — like changing the color of each individual key — you’ll need to use Java, not XML.

Another common feature of keyboards is to add sounds on each click. You can do this easily by adding a new method in your service and calling itonKey.

The nice thing is that Android actually provides some sounds for us ready to use, so we can do this very easily:

Now just useplaySound()at the top of theonKeymethod and make sure to create a vibrator and audio manager (private AudioManager am; private Virbator v;). You could just as easily swap out the key sounds for your own in the assets folder, or change the duration and behavior of the virbration.

Closing comments

Now you have your very own custom keyboard! Another challenge ticked off of your Android development list. Play around with different key sizes, customization, and features to create the perfect typing experience.

Be sure to share your finished products in the comments down below! Happy text-inputting!

Thank you for being part of our community. Read ourComment Policybefore posting.