Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Emoctions with chat

Tags:

android

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.

like image 873
Gaurav Arora Avatar asked Dec 15 '22 14:12

Gaurav Arora


1 Answers

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

like image 50
Gaurav Arora Avatar answered Dec 30 '22 05:12

Gaurav Arora