Gaurav Arora
Gaurav Arora

Reputation: 8362

Sending Emoctions with chat

I have developed a chat application in android and I want to add the Emoctions feature with my application. I have traversed following links:

emoticons in message on Android

How to display emoticons in chat in android?

Android Chat application smiley

but have not found any good solution in between.

Please guide me with a good hyperlink or solution.

Scenario: a person can select any emoction out of the present emoctions and it should be also received on the receivers end.

Upvotes: 4

Views: 3997

Answers (3)

Gaurav Arora
Gaurav Arora

Reputation: 8362

Here is the Smileys adapter that I am using to get my problem solved :

public class SmileysAdapter extends BaseAdapter {

    private ArrayList<String> arrayListSmileys = new ArrayList<String>();
    private Context context;
    private HashMap<String, Integer> emoticons = new HashMap<String, Integer>();
    private ArrayList<String> showListSmileys = new ArrayList<String>();


    public SmileysAdapter(ArrayList<String> arraylistSmileys,Context context,HashMap<String, Integer> emoticons) {
        // TODO Auto-generated constructor stub

        this.arrayListSmileys = arraylistSmileys;
        this.context = context;
        this.emoticons = emoticons;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return arrayListSmileys.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return arrayListSmileys.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(context).inflate(R.layout.row, null);
        ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
        imageView.setBackgroundResource(emoticons.get(arrayListSmileys.get(position)));
        return convertView;
        }
}

Upvotes: 2

Gaurav Arora
Gaurav Arora

Reputation: 8362

I have solved it. Here is my code:

Move like this way:

private HashMap<String, Integer> emoticons = new HashMap<String, Integer>();
private ArrayList<String> arrayListSmileys = new ArrayList<String>();
ImageView imgbtn_show_smileys;

in your oncreate function proceed like this

emoticons.put(":-)", R.drawable.smile);
emoticons.put(":P", R.drawable.tongue);
emoticons.put(":D", R.drawable.cool);
emoticons.put(":-(", R.drawable.sad);
emoticons.put(":0", R.drawable.cool);
fillArrayList();

Make your own set of images/smileys pairs which you want to use

fill the array list

private void fillArrayList() {
    Iterator<Entry<String, Integer>> iterator = emoticons.entrySet().iterator();
    while(iterator.hasNext()){
        Entry<String, Integer> entry = iterator.next();
        arrayListSmileys.add(entry.getKey());
    }
}

Now the most important part, every time to set the image to list view or edittext use this function as :

edittext.setText(getSmiledText(this,"your text"));

public Spannable getSmiledText(Context context, String text) {
        SpannableStringBuilder builder = new SpannableStringBuilder(text);
        int index;
        for (index = 0; index < builder.length(); index++) {
            for (Entry<String, Integer> entry : emoticons.entrySet()) {
                int length = entry.getKey().length();
                if (index + length > builder.length())
                    continue;
                if (builder.subSequence(index, index + length).toString().equals(entry.getKey())) {
                    builder.setSpan(new ImageSpan(context, entry.getValue()), index, index + length,
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    index += length - 1;
                    break;
                }
            }
        }
        return builder;
    }

To show the smileys on the dialog button

imgbtn_show_smileys.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            final Dialog groupIconsDialog = new Dialog(UserChatActivity.this);
            groupIconsDialog.setTitle("Choose Group Icon");
            groupIconsDialog.setContentView(R.layout.group_icons_layout);
            //calling and setting the image icons to the grid view adapter
            final GridView groupIconsGrid = (GridView)groupIconsDialog.findViewById(R.id.grid_groupIcons);
            groupIconsGrid.setAdapter(new SmileysAdapter(arrayListSmileys, UserChatActivity.this, emoticons));


            groupIconsGrid.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int position, long arg3) {
                    // TODO Auto-generated method stub

                    String value = groupIconsGrid.getAdapter().getItem(position).toString();
                    value = editMessage.getText()+value;
                    Spannable spannable = getSmiledText(UserChatActivity.this, value);
                    editMessage.setText(spannable);
                    groupIconsDialog.dismiss();


                }
            });

            groupIconsDialog.show();

        }
    });

Let me know if you have any doubts regarding the same.

Thanks

Upvotes: 11

Hanny Udayana
Hanny Udayana

Reputation: 388

Well, basically the emoticons are processed like this. Every emoticons are based on a character combination. You can make a variety of image with their corresponding character combination. Let's make a scenario with this.

  1. Device A sent a smile image emoticons which is represented with :)
  2. The emoticons are sent to device B via server (or by any method)
  3. Device B received a string :) and then replace it with the smile image emoticons

Upvotes: -1

Related Questions