How to create an Input Method android?

3.5k views Asked by At

I want to create a generic keyboard like Emoji . I am following this tutorial.

I want the main softkeyboard to work as it is or I mean extends it. Only want to change layout and events on mModeChangeKey clicked when symbols shows.

When mode is changed i want to display my keyboard with emoticons. and just add it as others emoticons do.

Manifest.xml

  <service android:name="com.example.keyboardtesting.MyInputMethod"
        android:label="@string/app_name"
        android:permission="android.permission.BIND_INPUT_METHOD">
             <intent-filter>
                   <action android:name="android.view.InputMethod" />
             </intent-filter>

             <meta-data android:name="android.view.im"
                android:resource="@xml/method" />
    </service>

also added permissions..

first button click

startActivityForResult( new Intent(android.provider.Settings.ACTION_INPUT_METHOD_SETTINGS), 0);

Second button click

InputMethodManager inputmethodmanager = (InputMethodManager) getSystemService("input_method");

    if (inputmethodmanager != null)
    {
        inputmethodmanager.showInputMethodPicker();
    }

Because it is upto the user to select both . we cannot do it programatically . I also go through latin and softkeyboard . but i am still confuse.

MyInputMethod

public class MyInputMethod extends InputMethodService 
{
    private Keyboard mKeyboard;

    private KeyboardView mInputView;

    @Override 
    public void onInitializeInterface() 
    {
        mKeyboard = new Keyboard(this, R.xml.qwerty);
    }

    @Override
    public View onCreateInputView() 
    {
         mInputView = (KeyboardView) getLayoutInflater().inflate(
                 R.layout.input_black, null);

         mInputView.setKeyboard(mKeyboard);

         return mInputView;
    }
}

Simple Words:

I just want to show my keyboard on android device. I will add events later.

1

There are 1 answers

0
Srujan Barai On

If you want want to make sure that your keyboard shows, you need to add other files as well. Like this.

Create method.xml file

<?xml version="1.0" encoding="utf-8"?>
<input-method xmlns:android="http://schemas.android.com/apk/res/android">
<subtype
    android:label="@string/subtype_en_US"
    android:imeSubtypeLocale="en_US"
    android:imeSubtypeMode="keyboard" />
</input-method>

Make a layout of your keyboard. layout/keyboard.xml

<?xml version="1.0" encoding="UTF-8"?>
<android.inputmethodservice.KeyboardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/keyboard"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:keyPreviewLayout ="@layout/preview"
/>

The keyPreviewLayout is the layout of the short-lived pop-up that shows up whenever a key on the keyboard is pressed.

layout/preview.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:background="#ffff00"   
    android:textStyle="bold"
    android:textSize="30sp"
    >    
</TextView>

Next you need to make qwerty.xml for functionality. And you have already created the service class. But you have not implemented OnKeyboardActionListener. Make sure you do.