Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8302043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:11:18+00:00 2026-06-08T17:11:18+00:00

I am doing a simple chat application and i want to show smileys on

  • 0

I am doing a simple chat application and i want to show smileys on edittext while writing the message.

I have this to identify wich characters will be subsituted by an Image throught an ImageSpan (this is called only when an smileys character is inserted on EditText):

for (index = start; index < start+num_chars; index++) {
        if (index + 1 > editable.length())
            continue;  
          if(emoticons.containsKey(editable.subSequence(index, index + 1).toString())){
            int length=1; 

            Drawable drawable = context.getResources().getDrawable(emoticons.get(editable.subSequence(index, index + 1).toString()));
            Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();


            int size=Utils.GetDipsFromPixel(context, (int)(textSize*1.3));

            Drawable d = new BitmapDrawable(Bitmap.createScaledBitmap(bitmap, size, size, true));
            int dWidth = d.getIntrinsicWidth();
            int dHeight = d.getIntrinsicHeight();

            d.setBounds(0 , -dHeight, dWidth, 0);
            ImageSpan span;
            span = new ImageSpan(d,ImageSpan.ALIGN_BASELINE);
            editable.setSpan(span, index, index + length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            index += length - 1;
          }

      }

I am using SPAN_EXCLUSIVE_EXCLUSIVE tag to set the span, but i have problems with swiftkey keyboard because when i insert an smiley in the edittext, everything i write just after the imageSpan it keeps below the image (like SPAN_EXCLUSIVE_INCLUSIVE). With the Android default keyboard i havent this problem.

I only want whatsapp application same behaviour whit smileys on EditText.

Any suggestions? Any change i have to do to my code?

EDIT: “editable” variable is passed to the method. It is txtMessage.getText() value where txtMessage is an EditText.

Thanks!

EDIT: Only Span one portion of code! This works good in multiline! I think the problem was in using Drawable->Bitmap->ResizedBitmap->Drawable.

public static final HashMap<String, Integer> emoticons = new HashMap();
static {
    emoticons.put("\ue415", R.drawable.e415);
    emoticons.put("\ue056", R.drawable.e056);
    emoticons.put("\ue057", R.drawable.e057);
...
public static Spannable getSmiledText(Context context, Spannable editable,
        int start, int num_chars, float textSize) {

    int index;
    for (index = start; index < start + num_chars; index++) {
        if (index + 1 > editable.length())
            continue;
        if (EmojiLayout.emoticons.containsKey(editable.subSequence(index,
                index + 1).toString())) {
            int length = 1;

            Bitmap smiley = BitmapFactory.decodeResource(context.getResources(), ((Integer) EmojiLayout.emoticons.get(editable.subSequence(index,
                    index + 1).toString())));
            int size = Utils.GetDipsFromPixel(context,
            (int) (textSize * 1.37));

            Bitmap scaledbmp=Bitmap.createScaledBitmap(
                    smiley, size, size, false);
            ImageSpan span;
            span = new ImageSpan(scaledbmp);
            Log.d("EmojiLayout", "Index: " + String.valueOf(index) + "To: "
                    + String.valueOf(index + length));
            editable.setSpan(span, index, index + length,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            index += length - 1;
        }
    }
    return editable;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T17:11:19+00:00Added an answer on June 8, 2026 at 5:11 pm

    Use this one ::

        public static CharSequence addSmileySpans(Context ch, CharSequence your_recieved_message)
    {
        //smilyRegexMap = new HashMap<Integer, String>();
    
    private static final HashMap<String, Integer> smilyRegexMap = new HashMap<String, Integer>();
    smilyRegexMap.put( ">:-\\(" , R.drawable.fb_grumpy);
            smilyRegexMap.put( ">:\\(" , R.drawable.fb_grumpy);
            smilyRegexMap.put( ">:-O" , R.drawable.fb_upset);
            smilyRegexMap.put( ":-\\)" , R.drawable.fb_smile);
            smilyRegexMap.put( ":\\)",R.drawable.fb_smile);
            smilyRegexMap.put( ":-\\]" , R.drawable.fb_smile);
            smilyRegexMap.put( ":-\\(", R.drawable.fb_frown);
    
    
    
    
        System.out.println("==In Spannable Function..........");
        SpannableStringBuilder builder = new SpannableStringBuilder(your_recieved_message);
    
        System.out.println("==================Size of Smily  : "+ smilyRegexMap.size());
    
        @SuppressWarnings("rawtypes")
        Iterator it = smilyRegexMap.entrySet().iterator();
        while (it.hasNext()) {
            @SuppressWarnings("rawtypes")
            Map.Entry pairs = (Map.Entry) it.next();
    
    
    
    
            Pattern mPattern = Pattern.compile((String) pairs.getKey(),Pattern.CASE_INSENSITIVE);
            Matcher matcher = mPattern.matcher(your_recieved_message);
    
            while (matcher.find()) {
    
                    Bitmap smiley = BitmapFactory.decodeResource(ch.getResources(), ((Integer) pairs.getValue()));
                    Object[] spans = builder.getSpans(matcher.start(), matcher.end(), ImageSpan.class);
                    if (spans == null || spans.length == 0) {
                        builder.setSpan(new ImageSpan(smiley), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }
            }
        }
        return builder;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple chat application and I want to show balloons similar
I'm doing a simple chat. When user sends message to server I want to
I've been doing simple multi-threading in VB.NET for a while, and have just gotten
i have been doing simple MVC tutorials for a while and i get the
I'm doing a simple chat script and I have the killSession function which kills
I want to build a simple web-interface application that can send/receive chat messages to/from
While doing simple program I noticed this issue. int[] example = new int[10]; List<Integer>
I'm writing a simple application which will connect with a server. However I want
I was doing a simple c++ chat and i wanted to encrypt the messages
In My iphone Application,I am doing simple image animation in a View and in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.