22

I have been trying for the last 3 days to show a graphical custom emoticon in the default android message view. I have successfully shown my custom emoticons in place of keys. Now the problem is that I am trying to show a drawable in spanable string builder. But the drawable just does not appear on the keyboard. Here is the code so far:

     SpannableString ss = new SpannableString(" "); 
                    Drawable d = getResources().getDrawable(R.drawable.a); 
                    d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); 
//                  ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE); 
                    ImageSpan span = new ImageSpan(d);
//                  ss.setSpan(span, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

                    mComposing.append(":");
                    mComposing.setSpan(new ImageSpan(d), 0,1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    getCurrentInputConnection().commitText(mComposing, 1);

I have tried different methods to somehow fit the drawable but it just wont show on the default message view of android. Any help would be highly appreciated.

Mickey
  • 943
  • 1
  • 19
  • 41
SoH
  • 2,180
  • 2
  • 24
  • 53
  • Hey SoH, Did you find a solution for your issue? I also have same issue when I am going to add image to Android Messaging app. If you have an answer, please post it as Answer of this thread. Thanks.. – harsh Feb 28 '13 at 03:18
  • I am doing something similar HERE!!! http://stackoverflow.com/questions/16768930/implementations-of-emoji-emoticon-view-keyboard-layouts – Etienne Lawlor May 27 '13 at 16:51
  • Could you clarify what you mean by "the default message view of android?" What class isn't properly displaying the ImageSpan, and where did the instance come from? – Erhannis Sep 04 '13 at 12:27

3 Answers3

1

May be this will be helpful: Emoticons-Keyboard

See this also: Implementations of Emoji (Emoticon) View/Keyboard Layouts

Community
  • 1
  • 1
captaindroid
  • 2,868
  • 6
  • 31
  • 45
  • Note that [link-only answers](http://meta.stackoverflow.com/tags/link-only-answers/info) are discouraged, SO answers should be the end-point of a search for a solution (vs. yet another stopover of references, which tend to get stale over time). Please consider adding a stand-alone synopsis here, keeping the link as a reference. – kleopatra Sep 02 '13 at 12:13
0

Maybe you could use:

String txt = "<img src=\"" + resourceID + "\"/>"; to generate an HTML tag and after that

Spanned spanned = Html.fromHtml(txt, emojiGetter, null);
editTextObj = setText(spanned,BufferType.SPANNABLE);

where emojiGetter is

private ImageGetter emojiGetter = new ImageGetter() {
    public Drawable getDrawable(String source){
        int id = getResources().getIdentifier(source, "drawable", context.getPackageName());

        Drawable emoji = getResources().getDrawable(id);
        int w = (int)emoji.getIntrinsicWidth() ;
        int h = (int)emoji.getIntrinsicHeight() ;
        emoji.setBounds(0, 0, w, h);
        return emoji;
    }
};

It works fine for me. I've created a CustomEditText component to simplify this conversion.

iflorit
  • 740
  • 5
  • 10
  • Hey iflorit, I also has the same issue which an image doesn't display in Android messaging app. I created a custom keyboard using SOftKeyboard sample in SDK. I tried your code but it only display a small square with OBJ text, not my image. I noticed that in your code, you have set the image to a EditText. But in Android messaging app, we can't access its EditText. But in custom keyboard I insert letters to Messaging app using SoftKeyboard sample's StringBuilder object. Is there any way to insert images to messaging app using that StringBuilder object? Thanks in Advnce..!! – harsh Feb 28 '13 at 03:16
  • No, It is not a solution. You have to get the unicode for all the emojis and print them. – Er Rahul Rajkumar Gupta Aug 22 '13 at 06:33
0

i have achieved it doing like this

 ImageGetter imageGetter = new ImageGetter() 
                {
                    public Drawable getDrawable(String source) {
                        Drawable d = getResources().getDrawable(R.drawable.e041);
                        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
                        return d;
                    }
                };

                Spanned cs = Html.fromHtml("<img src='" + getResources().getDrawable(R.drawable.e041) + "'/>", imageGetter, null);

             getCurrentInputConnection().commitText(cs, 1); 

//but it override the last entered text see my question here

Community
  • 1
  • 1
Xar-e-ahmer Khan
  • 1,314
  • 15
  • 23