Using Images in a Custom IME

629 views Asked by At

I have been attempting to implement a custom SoftKeyboard (IME) which contains and outputs icons for keys. Think emoji. I have skinned the keys, but injecting the selected drawable into the String Builder has proven difficult. I decided to implement my own ImageGetter so that I could control what was returned by Html.fromHtml(). The fromHtml method continues to act as if it's returning null even though I know that the drawable is not null.

Any help would be greatly appreciated.

I based this project off of the SoftKeyboard sample. I targeted the file, SoftKeyboard.java an instance of

public class SoftKeyboard extends InputMethodService 
    implements KeyboardView.OnKeyboardActionListener 

This class instantiates an object mComposing, this is used to store the text entered into the edit box from the soft keyboard.

private StringBuilder mComposing = new StringBuilder();

I thought that I could append the images as a Spanned to the StringBuilder and display the image that way. What happens here is that a white square with "obj" text in it shows instead of the selected icon, meaning getDrawable() is returning null.

I have tried a few things with the bounds but I am not sure if I am close or if I am going down the wrong path.

Declaring my drawable in onCreate:

 mHangingDrawable = getResources().getDrawable(R.drawable.hanging_tiny);
 mHangingDrawable.setBounds(0, 0, mHangingDrawable.getIntrinsicWidth(), mHangingDrawable.getIntrinsicHeight());

In handleCharacter(int, int[]) I wrote this catch for codes that I have personally mapped.

else if (primaryCode == 50) {
        String htmlSource = "<img src='hanging_small' />";
        ImageGetter ig = new ImageGetter(){

            @Override
            public Drawable getDrawable(String source) {


                return mHangingDrawable;
            }

        };
        Spanned htmlSpan = Html.fromHtml(htmlSource, ig, null);
        mComposing.append(htmlSpan);

        getCurrentInputConnection().commitText(mComposing, 1);

}

This article about Creating Input Methods was a helpful resource.

0

There are 0 answers