Sending emotions with chat
I have developed a chat application in android and I want to add Emoctions function to my application. I followed the links:
emoticons in message on Android
How to display emoticons in chat in android?
but no good solution has been found between them.
Please advise with a good hyperlink or solution.
Scenario: A person can choose any emotion from real emotions and must also be received at the end of the receivers.
source to share
I solved it. Here is my code:
Move as follows:
private HashMap<String, Integer> emoticons = new HashMap<String, Integer>();
private ArrayList<String> arrayListSmileys = new ArrayList<String>();
ImageView imgbtn_show_smileys;
in your oncreate function do the following
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();
Create your own set of image / emoji pairs 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 you set an image for a list view or edittext use this function like:
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 emoticons on a 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 doubts about the same.
thank
source to share
Here is the Smileys adapter I'm using to solve my problem:
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;
}
}
source to share
Well, basically emoticons are handled like this. Each emoticon is based on a combination of characters. You can make a variety of images with the appropriate combination of symbols. Let's make a script with this.
- Device A has sent smiley images smileys which are presented :)
- Smartphones are sent to device B via the server (or any way).
- Device B received the string :) and then replaced it with emoji emoticons
source to share