Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert text to emoticon in android

HI in my app i use this class to change my text to emoticon .

public class MainActivity extends Activity {

private static final Factory spannableFactory = Spannable.Factory
        .getInstance();

private static final Map<Pattern, Integer> emoticons = new HashMap<Pattern, Integer>();

static {
    addPattern(emoticons, ":)", R.drawable.ic_launcher);
    addPattern(emoticons, ":-)", R.drawable.ic_launcher);
    // ...
}

private static void addPattern(Map<Pattern, Integer> map, String smile,
        int resource) {
    map.put(Pattern.compile(Pattern.quote(smile)), resource);
}

public static boolean addSmiles(Context context, Spannable spannable) {
    boolean hasChanges = false;
    for (Entry<Pattern, Integer> entry : emoticons.entrySet()) {
        Matcher matcher = entry.getKey().matcher(spannable);
        while (matcher.find()) {
            boolean set = true;
            for (ImageSpan span : spannable.getSpans(matcher.start(),
                    matcher.end(), ImageSpan.class))
                if (spannable.getSpanStart(span) >= matcher.start()
                        && spannable.getSpanEnd(span) <= matcher.end())
                    spannable.removeSpan(span);
                else {
                    set = false;
                    break;
                }
            if (set) {
                hasChanges = true;
                spannable.setSpan(new ImageSpan(context, entry.getValue()),
                        matcher.start(), matcher.end(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }
    return hasChanges;
}

public static Spannable getSmiledText(Context context, CharSequence text) {
    Spannable spannable = spannableFactory.newSpannable(text);
    addSmiles(context, spannable);
    return spannable;
}

OnClickListener listener1 = new OnClickListener() {
    @Override
    public void onClick(View v) {

        EditText tx = (EditText) findViewById(R.id.editText1);
        tx.getText().insert(tx.getSelectionStart(), getSmiledText(getBaseContext(), ":-)"));

        }};




@Override
protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.activity_main);




    Button btn = (Button) findViewById(R.id.button1);
     btn.setOnClickListener(listener1);


}

Q1 : i clicked on this Button and show emoticon in edittext . now i have 400 emoticon , i should create 400 Button? or this is not best way ?

Q2 : in app when i clicked on EditText , SelectionStart() is 1 or 0 and begin typing from first line . how to change selectionstart to everywhere user touched in edittext like line 3 of edittext ?

like image 331
Eli Avatar asked Sep 29 '22 22:09

Eli


1 Answers

Use built in library

https://github.com/ankushsachdeva/emojicon

https://github.com/rockerhieu/emojicon

or create a custom IME

https://github.com/AnySoftKeyboard/AnySoftKeyboard

fell free if you have confusion in creating a custom Softkeyboard

like image 168
Zar E Ahmer Avatar answered Oct 05 '22 08:10

Zar E Ahmer