2

I am currently working on a simple chat application. I want to add emoticons features in this app. I already have a function to receive string from other users. What I want is- If the text that user sent contains CharSequence like ":D" or ":p", I want them to be changed into the emoticons. How can I implement this function?

I want a function like this:

public void updateMessage(){
    if (receivedMessage.contains(":D")){
        receivedMessage.replace(":D",image);
    }
    messageLog.append(receivedMessage);
}

Please help me if this is possible. If there are other ways, please mention them.

Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
  • This is not a duplicate. Whoever marked it as such made an error. – Karakuri Aug 12 '15 at 15:27
  • @Karakuri What can I do now? I am new user in this forum. – Nabin Bhandari Aug 12 '15 at 15:29
  • I think the only thing you can do is ask again. Maybe link to this one and state specifically that it was incorrectly marked as duplicate and is different from the one linked to by @Mena – Karakuri Aug 12 '15 at 15:54
  • @Karakuri I still think this is a duplicate, if you look at the question I'm pointing out. But maybe I'm missing a point, in which case I'll be happy to reopen. – Mena Aug 12 '15 at 16:03
  • @karakuri Thanks for the reply. I will try that after 90 minutes. I can ask only after 90 minutes. I don't khow why stackoverflow is being very strict to new users. – Nabin Bhandari Aug 12 '15 at 16:06
  • @Mena can you please point me directly to the answer you think is correct? – Nabin Bhandari Aug 12 '15 at 16:07
  • @Nabin I'd say the accepted answer for the question I'm pointing to looks like what you want. – Mena Aug 12 '15 at 16:09
  • @Mena if i use getDrawable() function, android studio forces me to use Lollipop as target API. As you know there are only few lollopop users. Please give me alternative for that. – Nabin Bhandari Aug 12 '15 at 16:11
  • That's weird, as `Resources#getDrawable(int id)` should be deprecated in Lollipop. Anyway, you can find your way through the compatibility by looking at the official docs, or somewhere else in SO - there's plenty of literature. – Mena Aug 12 '15 at 16:18

2 Answers2

2

You can try like this

 if (rosan.contains(":p")){
            int span = abc.indexOf(":p");
            res = getResources().getDrawable(R.drawable.myImage); 
            res.setBounds(0, 0, res.getIntrinsicWidth(), res.getIntrinsicHeight());
            span = new ImageSpan(res, ImageSpan.ALIGN_BASELINE);
            ss.setSpan(span, startSpan, startSpan+2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            rosan = rosan.replaceFirst(":D","  "); //replace with two blank spaces.
        }
Roshan Sharma
  • 513
  • 2
  • 7
  • 20
1

It can easily be implemented by using SpannableString Here is a sample implementation in code.

package com.jslan.nabin.emosample;

import android.graphics.drawable.Drawable;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.widget.EditText;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText textBox = (EditText) findViewById(R.id.textBox);//I have created an EditText named textBox in activity_main.xml
        Drawable d;
        ImageSpan span;
        String abc = "some :D message :D";
        SpannableString ss = new SpannableString(abc);
        for(int i = 0; i<abc.length();i++){//loop to check presence of other emoji strings.
            if (abc.contains(":D")){
                int startSpan = abc.indexOf(":D");
                d = getResources().getDrawable(R.drawable.laugh); // I have kept laugh.png of size 24x24 in drawable folder.
                d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
                span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
                ss.setSpan(span, startSpan, startSpan+2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                abc = abc.replaceFirst(":D","  "); //replace with two blank spaces.
            }
            //to check other types of emoji, use else if ladder and place the similar code.
        }
        textBox.append(ss);
    }
}
Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59