3

I am developing a chat application in which I want to add smiley support to it. But I am not being able to use it.
I have gone through many codes but the symbols are not changing to smileys.
Here is my code...

private static final Factory spannableFactory = Spannable.Factory
        .getInstance();

private static final Map<Pattern, Integer> emoticons = new HashMap<Pattern, Integer>();

static {
    addPattern(emoticons, ":)", R.drawable.emo_im_happy);
    addPattern(emoticons, ":-)", R.drawable.emo_im_happy);
    // ...
}

private static void addPattern(Map<Pattern, Integer> map, String smile,
        int resource) {
    map.put(Pattern.compile(Pattern.quote(smile)), resource);
}
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.messaging_screen); // messaging_screen);

    messageHistoryText = (EditText) findViewById(R.id.messageHistory);

    messageText = (EditText) findViewById(R.id.message);

    messageText.requestFocus();

    sendMessageButton = (Button) findViewById(R.id.sendMessageButton);
}

public void appendToMessageHistory(String paramString1, String paramString2) {
        if ((paramString1 != null) && (paramString2 != null)) {
            if (paramString1.toString().equals(this.friend.userName.toString())) {
                appendColoredText(this.messageHistoryText,paramString1 + ":\n", Layout.Alignment.ALIGN_NORMAL,-16711936);
                appendColoredText(this.messageHistoryText, getSmiledText(this, paramString2) + "\n",    Layout.Alignment.ALIGN_NORMAL, -16711936);


            } else {
                appendColoredText(this.messageHistoryText, "you:\n",Layout.Alignment.ALIGN_OPPOSITE, -65536);
                appendColoredText(this.messageHistoryText, getSmiledText(this, paramString2) + "\n",Layout.Alignment.ALIGN_OPPOSITE, -65536);


            }
        }
    }

    public static boolean addSmiles(Context context, Spannable spannable) {
        boolean hasChanges = false;
        for (Entry<Pattern, Integer> entry : emoticons.entrySet()) {
            Matcher matcher = entry.getKey().matcher(spannable);
            while (matcher.find()) {
                boolean set = true;
                for (ImageSpan span : spannable.getSpans(matcher.start(),
                        matcher.end(), ImageSpan.class))
                    if (spannable.getSpanStart(span) >= matcher.start()
                            && spannable.getSpanEnd(span) <= matcher.end())
                        spannable.removeSpan(span);
                    else {
                        set = false;
                        break;
                    }
                if (set) {
                    hasChanges = true;
                    spannable.setSpan(new ImageSpan(context, entry.getValue()), matcher.start(), matcher.end(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }
        return hasChanges;
    }


    public static Spannable getSmiledText(Context context, CharSequence text) {
        Spannable spannable = spannableFactory.newSpannable(text);
        addSmiles(context, spannable);
        return spannable;
    }


    public static void appendColoredText(EditText paramEditText,String paramString, Layout.Alignment paramAlignment, int paramInt) {
        int i = paramEditText.getText().length();
        paramEditText.append(paramString);
        int j = paramEditText.getText().length();
        Editable localEditable = paramEditText.getText();
        localEditable.setSpan(new ForegroundColorSpan(paramInt), i, j, 0);
        localEditable.setSpan(new AlignmentSpan.Standard(paramAlignment), i, j, 0);
        //localEditable.setSpan(new BackgroundColorSpan(back), i, j, 0);

    }

I am using this code with Static block so that the emoticons should be displayed but they are not being displayed. So what should I do to get it done?

Thank You

Tanmay Patil
  • 6,882
  • 2
  • 25
  • 45
Sanket Naik
  • 173
  • 2
  • 16
  • possible duplicate http://stackoverflow.com/questions/3341702/displaying-emoticons-in-android/4302199#4302199 – Karan Thakkar Mar 18 '14 at 10:19
  • yep sort of but I am not able to use it in my code so please provide me with solution @karanthakkar instead of just adding comment – Sanket Naik Mar 18 '14 at 10:20
  • I have used http://stackoverflow.com/questions/3341702/displaying-emoticons-in-android/4302199#4302199 and it's worked for me. If you wan't your text automatically change :) to smiley so you need some listener on EditText I think. Yes? or when you call getSmiledText? – Nadir Novruzov Aug 29 '14 at 11:42

0 Answers0