15

hello i am developing chat application in which i want to insert smiley i have not much idea about it how to integrate and display in it can u give me suggestion for doing the same ?

ImageGetter imageGetter = new ImageGetter() {
    public Drawable getDrawable(String source) {
        Drawable d = getResources().getDrawable(
                R.drawable.happy);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        return d;
    }
};

cs = Html.fromHtml(
        "<img src='"
                + getResources()
                        .getDrawable(R.drawable.happy)
                + "'/>", imageGetter, null);
System.out.println("cs is:- " + cs);
edttxtemoji.setText(cs);

i found this code, in this it uses images, is this feasible ? or there is another solutions ? please give me better solution for this thanx in advance

Ramesh Solanki
  • 2,961
  • 4
  • 28
  • 44
  • See solution using ImageSpan with EditText: http://stackoverflow.com/questions/19534427/edittext-with-spannablestringbuilder-and-imagespan-doesnt-works-fine/19649371#19649371 – Bismark Ito Oct 30 '13 at 16:06

3 Answers3

8

Yes there is another way for showing smiley within the TextView or EditText. Build a Spannable text using ImageSpanand then setText the Spannable to TextView or EditText. Here is an post for the same.

Community
  • 1
  • 1
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
1

To set Smiley in edittext

int value=R.id.ic_launcher;
Drawable Smiley = getResources().getDrawable(value);
Smiley.setBounds(0, 0, 15, 15);
SpannableStringBuilder builder = new SpannableStringBuilder();
String imgValue = "["+value+"]";
builder.append(imgValue);
builder.setSpan(new ImageSpan(Smiley), builder.length()-imgValue.length(), builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
edit_text.getText().insert(txt.getSelectionStart(), builder);

now to fetch smiley in listview or textview

textview.setText(addSmileySpans(context,edit_text.getText()));
public CharSequence addSmileySpans(Context context, CharSequence msg) {

    SpannableStringBuilder builder = new SpannableStringBuilder(your_recieved_message);
    Pattern pattern = Pattern.compile("\\[([^\\[\\]]+)\\]");
    if( pattern != null )
    {
        Matcher matcher = pattern.matcher( your_recieved_message );
        int matchesSoFar = 0;
        while( matcher.find() )
        {
            CharSequence cs =matcher.group().subSequence(1, matcher.group().length()-1);
            int value = Integer.parseInt(cs.toString());
            System.out.println("pattern is::"+matcher.group().subSequence(1, matcher.group().length()-1)); 
            int start = matcher.start() - (matchesSoFar * 2);
            int end = matcher.end() - (matchesSoFar * 2);
            Drawable Smiley = context.getResources().getDrawable(value);
            Smiley.setBounds(0, 0,15,15);
            builder.setSpan(new ImageSpan(Smiley), start + 1, end - 1, 0 );
            builder.delete(start, start + 1);
            builder.delete(end - 2, end -1);
            matchesSoFar++;

        }
    }
    return builder;
}
Manthan Patel
  • 993
  • 1
  • 9
  • 20
1

I think it is little bit late.

public void addSmily() {
    CharSequence text = myEditText.getText();
    int resource  = R.drawable.ic_menu_emoticons ;
    myEditText.setText(getSpannableText(text,resource));
}

private Spannable getSpannableText(CharSequence text, int smilyToAppend) {
    Spannable spannable = Factory.getInstance().newSpannable(text+" ");
    ImageSpan smilySpan = new ImageSpan(getApplicationContext(),smilyToAppend);
    spannable.setSpan(smilySpan, spannable.length()-1, spannable.length(), 0);
    return spannable;
}
shobhan
  • 1,460
  • 2
  • 14
  • 28