How to send emojicons icons when click on button i

2019-04-15 01:49发布

1.I am making an chatting application, in which during chatting, I want to send emoticons icons when selects from the dialog and then click on send button, it should show that particular image.

2.when I am clicking on send button, it's sending to user and shows, but the problem is that I want to show the image on the place of a name, but it's showing me like this

enter image description here

like "4.png" or "2.png". I want to display the image which I have selected. What should I do? I am confused, please help me.here is my code

 smilee.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View arg0) 
        {

            if (!popupWindow.isShowing()) 
            {
                popupWindow.setHeight((int) (keyboardHeight));

                if (isKeyBoardVisible) {
                    emoticonsCover.setVisibility(LinearLayout.GONE);
                } else {
                    emoticonsCover.setVisibility(LinearLayout.VISIBLE);
                }
                popupWindow.showAtLocation(parentLayout, Gravity.BOTTOM, 0, 0);

            } else {
                popupWindow.dismiss();
            }

        }});
    send.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View view) 
        {

            String text = mSendText.getText().toString().trim();

            Spanned sp = mSendText.getText();                   
            //chats.add(sp);
            //content.setText("");

            mSendText.setCompoundDrawables(null, null, getResources().getDrawable(R.drawable.s1), null);
            if(commanInstance.checkInternetConn(getApplicationContext()))
            {
                if(commanInstance.getConnection().isConnected())
                {
                    if(text.length()!=0)
                    {
                        Log.i("XMPPClient", "Sending text [" + text + "] to [" + to + "]");

                        Chat chat = null;
                        chat = commanInstance.getConnection().getChatManager().createChat(to, XMPPClient.this);
                        chat_imageview.setEnabled(true);

                        /*
                         * send msg
                         */
                        Message             message = new Message(chat.getParticipant(), Message.Type.chat);
                        message.setThread(chat.getThreadID());
                        String messagePacketID=message.getPacketID();
                        //message.setProperty("Time", commanInstance.getCurrentTime());
                        message.setThread(commanInstance.getCurrentTime());
                        message.setBody(text);

                        MessageEventManager.addNotificationsRequests(message, true, true, true, true);
                        try {
                            chat.sendMessage(message);
                            sendCancelledNotification(to);

                        } catch (XMPPException e1) {
                            e1.printStackTrace();
                        }
                        /*
                         * add msg in list item
                         */
                        /*
                         * insert into database
                         *
                         */
                            DatabaseHandler db = new DatabaseHandler(getApplicationContext());

                        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
                        Date date = new Date();

                        String[] toYou=to.split("@");




                        ItemTwoLine act=new ItemTwoLine(getResources().getDrawable(R.drawable.ic_launcher),changeNameToCapital(getUserName())+"#:#"+text+"#:#"+"Sent","Me"+"#:#"+commanInstance.getCurrentTime()+"#:#"+messagePacketID,true,getResources().getDrawable(R.drawable.check));
                        commanInstance.getCustomeList(listKeyValue).add(act);
                        int positionOfItem=commanInstance.getCustomeList(listKeyValue).indexOf(act);
                        db.addContact(new Contact(getUserName(),toYou[0],changeNameToCapital(getUserName())+"#:#"+text+"#:#"+"Sent","Me"+"#:#"+commanInstance.getCurrentTime()+"#:#"+messagePacketID,"Sent","Out","garvage","garvage",String.valueOf(positionOfItem),dateFormat.format(date)));



                        db.close();
                        /*
                         * Mantain open conversation screen list
                         */

                        //  String valForOpen=openCon[0]+"#:#"+text;

                        /*
                         * End open conversation screen list and start refersh list
                         */

                        if(t)
                        {
                            list.setAdapter(adapter);

                            adapter.notifyDataSetChanged();
                            t=false;
                        }
                        else
                        {
                            adapter.notifyDataSetChanged();
                        }
                        mSendText.setText("");
                        scrollMyListViewToBottom();
                        /*
                         * set alarm
                         */
                        //setAlarm(openCon[0]);
                        /*
                         * set alarm end
                         */


                    }
                }
            }
        }
    });

}

Here is my adapter class

Msg = (TextView) convertView.findViewById(R.id.Msg);
            //Msg.setText(Html.fromHtml(nameAndText[1] ));

            //               TextView textView2 = (TextView)findViewById( R.id.TextView2 );
            SpannableStringBuilder ssb = new SpannableStringBuilder(nameAndText[1]);
            //Bitmap smiley = BitmapFactory.decodeResource( getResources(), R.drawable.emoticon );

            //ssb.setSpan(smiley, 16, 17, Spannable.SPAN_INCLUSIVE_INCLUSIVE );    
            Msg.setText( ssb, BufferType.SPANNABLE );

标签: android xmpp
2条回答
淡お忘
2楼-- · 2019-04-15 02:23

You can use this library.

https://github.com/rockerhieu/emojicon

It has custom text views and controls which are capable to displaying the smileys and also handles converting them to unicode.

I used this with our chat application and it worked like a charm, even saving them in database is very easy.

查看更多
孤傲高冷的网名
3楼-- · 2019-04-15 02:36

use this code may help to send the emojisicon with the text

textview.setText(getSmiledText(text.toString()));

     public Spannable getSmiledText(String text) {
    SpannableStringBuilder builder = new SpannableStringBuilder(text);
    if (emoticons.size() > 0) {
        int index;
        for (index = 0; index < builder.length(); index++) {
            if (Character.toString(builder.charAt(index)).equals(":")) {
                for (Map.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(getContext(), entry.getValue()), index, index + length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        index += length - 1;
                        break;
                    }
                }
            }
        }
    }
    return builder;
}

first create the heap for the emoticons,try this.

private HashMap<String, Integer> emoticons = new HashMap<String, Integer>();

emoticons.put(":-)", R.drawable.f01);
emoticons.put(":P", R.drawable.f02);
emoticons.put(":D", R.drawable.f03);
查看更多
登录 后发表回答