Good Afternoon everyone, today you are going to learn how to build your own custom android visual keyboard, this tutorial is going to be covering most Android Components and exposing you to most of the basic techniques and practices that will help you build your Android Development Career.
ANDROID VISUAL KEYBOARD
PART 1: Getting Familiar With the Android Visual KeyBoard Api(Application Programming Interface).
The Android Visual KeyBoard Its that little screen that popups at the bottom of your smart phone the moment you click on your input fields “the rectangular box you see when you type text”. This visual keyboard screen is capable of communicating with your input fields by sending your typed characters to the field. In general what happen is, the effect of the key pressed by you is communicated to the input field of the user, which is then sent to the application like(text messaging app,whatsapp)design to respond to that event. The android visual keyboard is one of the most essential part of any android application, without its present most android apps are more or less redundant, see screenshots below:
Before the keys are pressed:
After the keys are pressed:
WHAT’S REALLY HAPPENING BEHIND THE SCENE WHEN WE TYPE TEXT
To understand how the Android keyboard works we first need to understand what are services, meta datas, permission, interfaces, Inputtypes.
A service is a utility program design to perform a set of predefined actions, for example lets use the restaurant as a case study. The waiter is there to perform the action of taking the orders of customers and performing the respective action base on those orders taken, here is what happens:
Without customers the waiter is not useful, there must be customers present for the waiter to perform the jobs of meeting to their demands, the waiter is a service, which perform the task of meeting the needs of customers and nothing more, there must be customer for a waiter to perform its function. The functions the waiter performs are supplied or given to him/her by the respective head of the restaurant or manager, meaning the waiter must perform all the instruction given to him/her by the manager else he/she will be fired.
This description is same analogy with the Android services, the keyboard is a service that inherits functions or orders to perform from the “InputMethodService” interface, in our case study the restaurant manager. I know you all must be puzzled about the word interface?
Interfaces are signed agreements by the waiter to perform the series of duty given to him/her by the restaurant manager. In software paradigm, interface are ways of performing some set of functions in unrelated classes; These classes can be likened to the waiter meeting different customers(classes) demands in different ways; Meaning same function can be executed to meet the desired goals of classes differently, based on the policy or guidelines(functions given to the waiter) laid down by the manager. These classes(customers) have access to the sets of function rendered by the waiter because they signed the agreement provided by these interfaces(paid for the functions or services).
In all restaurant apart from it being the duty of the waiter to respond to users need, there are some basic information that lets the restaurant security personnel know that the waiter is working in that particular restaurant, these basic information could be gotten by just viewing the waiter Identity card, same also is similar with the Android operating system. It cannot tell which is a service and which isn’t without first checking for this basic information inside the application. We provide this basic information via our application manifest folder and inside the meta tag.
All ID card have a name and extra information, our own Android ID card or formerly speaking meta tag, must have a name, and also a value or resource attribute to supply the necessary information needed by the system to identify our Keyboard Service or our waiter. With all this few points highlighted lets Jump straight into android studio and get our hands dirty.
ANDROID VISUAL KEYBOARD IMPLEMENTATION
Create a project with your own custom name, select the no activity template and also specify your minimum sdk requirement. We don’t need an activity for this project since Android keyboard is basically a service that runs in the background Thread.
A Thread in Android is a block or house which contains series of related programs, this programs are sent to the Android OS based on their priority or better how urgent the task they are designed to perform must be executed; An OS can be found to contain series of thread housing programs from different families.
For the time being we are only going to be concerned about adding just a service to our project,later on in this course we would design an activity for housing most of our keyboard basic settings and features. Screenshots on how to create a new project are shown below:
Note: We Don’t need an Activity for this project so select the Add No Activity template.
ANDROID MANIFEST FILE:
The Android Manifest is the most essential part of your Android project, it’s the first part the Android OS will look at when your Android Application compiled from this project and installed on a virtual or real device. Yea I guess you all might be wondering isn’t the Application and the Project the same entity? Well they aren’t that the same:
Project vs Application: Its a Folder housing sub folders and files which when compiled produces your Android Application , While An Application is the Compiled Form of a project, in which all your files and folders are compiled to create a living entity capable of achieving set out goals or objectives.
The Android Manifest Can be likened as the brain of your Android Application, and the Android OS interact with the rest of your Android Application by first communicating with your Android Manifest file. In the screenshot provided below expand your button, then your app button, locate your way to the folder named manifest, then click on the Android Manifest File that you see.
Note: The Android Manifest file is empty with just the Application tag inside housing the fundamental information about your project.
Now lets include a service into the Application Tag, to let the Android OS know our application is making use of a service to function. Check the screenshots below to see how our manifest looks now when we added an empty service(an empty service is a service with no information at all to work with).
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.evansabohi.com.afrikeys">
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
<service android:name="" android:label="" android:permission=" ">
According to the Android Documentation each service your application uses should be referenced in your manifest via the Service Tag, The Service tag contains a some sets of attributes which are explained below:
- The Name and Label Attribute: The Name attribute Helps The Android OS identify your service while the Label attribute is similar to the name attribute just that it helps phone users identify your service in the language and input panel of the phone setting screen.
- The Permission Attribute: Just as its name suggest, it’s a permission ; its more like you giving the Android OS the permissions that enable the OS to effectively call your service each time phone users wants to type some text.
That being said lets add our meta tag or informally waiter ID Card inside our service tag and also add our intent filter inside the Service tag.
INTENT FILTERS: Intent Filter just as its name suggest it’s a filter for distinguishing rice from beans , lol just joking but its quite true. The Android Intent Filter Class or tag is responsible for letting the Android OS know that our service is an Inputmethod service(for getting users inputs).
One thing you should know is that the OS thinks, yea it does and its even safe say the Android Os is a living Entity, Oooh……yea it is; By setting or giving the Android Os permission, it would be like: Ooh so I have the permission to call this service to attend to phone users if they wants to interact with the input field, but I don’t know how to distinguish this service from series of other service? Ooh yea the intent filter is a way off this confused state for the OS, it provides the OS the necessary information it needs to distinguish your service from all other service. The updated form of the Android Manifest File:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.evansabohi.com.afrikeys"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <service android:name=".AfriKeyboard" android:label="AfriCustomKeyBoard" android:permission="android.permission.BIND_INPUT_METHOD"> <meta-data android:name="android.view.im" android:resource="@xml/method"/> <intent-filter> <action android:name="android.view.InputMethod"/> </intent-filter> </service> </application> </manifest>
Remember we talked about providing some extra information like waiters name via our ID card or Meta tag, now it’s the time to provide that information via the Android resource Attribute of the meta tag.
Meta Tag Attributes:
- The Name Attribute: The meta tag name attribute is responsible for letting the Android OS know that we are dealing with an Input Method Service and also it should make use of the extra information provided in the resource attribute to effectively deal with the InputMethod Service.
- The Resource Attribute: The resource attribute house the extra information needed to enable the Android Os know how to deal with our service.
Well today we have learnt about how the Android keyboard works behind the scene and also exposed our self to most of the familiar terms we will be using as we go further in our Android Development Career.
In Part 2 we will be getting a feel of how the codes that power our android visual keyboard are implemented, each code block will be broken down to its respective pieces to enable easy digestion.