2

I'm working on a custom keyboard for Android, and I'm running into a problem. I need to either be able to output ASCII characters from the keyboard (i.e. ∅, ⌲, ⌓, etc), or be able to add images to the selected EditText from my keyboard. I know this is possible because Emoji does it with their keyboard. They have found a way to add images to any EditText, whether they created the EditText or not.

Does anyone have any idea how they accomplished this?

LukeWaggoner
  • 8,869
  • 1
  • 29
  • 28

3 Answers3

2

After many hours of pouring over blogs, Stackoverflow, and the rest of the internet, I've discovered that it's essentially impossible to add a custom image to an EditText unless you own that EditText.

All was not lost however. After looking through documentations on UTF-8, ASCII, and other character sets, I've discovered that all the characters I needed were in UTF-8. I simply output the unicode value using:

InputConnection inputConnection = getCurrentInputConnection();
inputConnection.commitText(text, 1);

and it correctly appears in the EditText. All the characters being added by other keyboards (i.e. stock Android, Emoji, etc.) appear in the UTF-8 character set.

LukeWaggoner
  • 8,869
  • 1
  • 29
  • 28
1

I came across this thread during some research and ended up elsewhere. Yes it is now possible to send images/GIF's to EditText's via a custom IME using the new CommitContent API introduced in 7.1 with backward compatibility till API level 13 - Honeycomb.

Quote from : https://developer.android.com/guide/topics/text/image-keyboard.html#how_it_works

enter image description here

With Android 7.1 (API level 25), the Android SDK includes the Commit Content API, which provides a universal way for IMEs to send images and other rich content directly to a text editor in an app. The API is also available in v13 Support Library as of revision 25.0.0. We recommend using the Support Library because it runs on devices as early as Android 3.2 (API Level 13), and it contains helper methods that simplify implementation.

I tested this with the latest update of hangouts and it works!.

Sample code here: https://developer.android.com/about/versions/nougat/android-7.1-samples.html#img-kbd-ime

I have merged the main app and the IME app code to a single project and here it is : https://github.com/satheeshwaran/ImageKeyboard-Android/tree/master

UPDATE:

I have published two apps using this API and I think they are doing good,

https://play.google.com/store/apps/details?id=com.theweekendparty.indianmemekeyboard

https://play.google.com/store/apps/details?id=com.theweekendparty.indiangifboardbollywood

Satheesh
  • 10,998
  • 6
  • 50
  • 93
  • When this question was posted and the answers were posted, that API hadn't been released. It's true, it's now possible. – LukeWaggoner Mar 29 '17 at 23:44
0

Use CompoundDrawables to add images to your EditText. You can add them through XML by android:drawableBottom android:drawableRight android:drawableLeft android:drawableTop. You can also use Spannables for displaying images inline with text inside EditText. See here Spannables and here Displaying emoticons in Android.

Community
  • 1
  • 1
michal.z
  • 2,025
  • 1
  • 15
  • 10
  • That's all fine and dandy, if I had access to the EditText itself. From what I can see, from the InputMethodService I've created, there is no way to actually access the selected EditText. I already know how to add images to an EditText. I need to be able to push them to an EditText that I don't own. – LukeWaggoner Dec 03 '14 at 18:57
  • You should have access to `InputConnection` through which you can pass `Spannables`. – michal.z Dec 03 '14 at 19:07
  • Tried that. The drawables never make it to the EditText. – LukeWaggoner Dec 03 '14 at 19:08
  • Hmm, many discussions on SO about this topic. See particularly this [Emoji](http://stackoverflow.com/questions/24645786/is-it-possible-to-add-animated-emoji-in-android-messaging). It seems that all you have to do is to simply send the encoded bytes of emoji through InputConnection then the smiling face should appear in the edit box. – michal.z Dec 03 '14 at 19:35
  • Ok, I understand that. Any ideas about how to send those encoded bytes through? Do you have to subclass BaseInputConnection? Is there another method on InputConnection for sending byte data? – LukeWaggoner Dec 04 '14 at 22:29