SoH
SoH

Reputation: 2190

Show Custom emoticon in Android Keyboard

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.

Upvotes: 22

Views: 5992

Answers (3)

Xar-e-ahmer Khan
Xar-e-ahmer Khan

Reputation: 1334

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

Upvotes: 0

captaindroid
captaindroid

Reputation: 2928

May be this will be helpful: Emoticons-Keyboard

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

Upvotes: 1

iflorit
iflorit

Reputation: 749

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.

Upvotes: 0

Related Questions