How to set Chips from string in EditText in Android
<p>I have an EditText widget in an Activity which creates text chip when the user presses space when typing. I save the result in comma separated values and store it in the database.</p>

<p><a href="https://i.stack.imgur.com/dMHVZ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dMHVZ.jpg" alt="enter image description here"></a></p>

<p>Now I'm trying to create another Activity which gets the data from the database and edit it. The max I'm able to do is set the text in the EditText.</p>

<p><a href="https://i.stack.imgur.com/nvzSk.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nvzSk.jpg" alt="enter image description here"></a>.</p>

<p>I'm stuck here. I'm able to create chips when the user types but not by code. What should I do to get the desired behavior?</p>

<p>My Code Below:</p>

<pre><code>edt.setText("Europe USA London Spain");
edt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(final CharSequence s, int arg1, int arg2, int arg3) {

                String addedString = s.toString();
                if (addedString.length() &gt; 0 &amp;&amp; addedString.endsWith(" ")) {
                    if (!isTextAdditionInProgress) {
                        // something is added
                        edt.setCursorVisible(true);
                        int startIndex = isContactAddedFromDb ? addedString.length() : edt.getSelectionEnd();
                        startIndex = startIndex &lt; 1 ? 1 : startIndex;
                        isTextAdditionInProgress = true;
                        addOrCheckSpannable(s, startIndex);
                    }
                    stringLength = addedString.length();
                } else {
                    Log.v("", "Doesn't contain space, good to go!");
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
                beforeChangeIndex = edt.getSelectionStart();
                changeString = s.toString();
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!isTextDeletedFromTouch &amp;&amp; s.toString().length() &lt; changeString.length() &amp;&amp; !isTextAdditionInProgress) {
                    String deletedString = "";
                    try {
                        deletedString = changeString.substring(afterChangeIndex, beforeChangeIndex);

                    } catch (Exception e) {
                    }
                    if (deletedString.length() &gt; 0 &amp;&amp; deletedString.contains(","))
                        deletedString = deletedString.replace(",", "");
                }
            }
        });




public void addOrCheckSpannable(CharSequence s, int startIndex) {
    boolean checkSpannable = false;
    String overallString;
    if (s == null) {
        checkSpannable = true;
        s = edt.getText();
        startIndex = edt.getSelectionEnd();
        startIndex = startIndex &lt; 1 ? 1 : startIndex;
        // startIndex =-1;
        overallString = edt.toString();
        if (TextUtils.isEmpty(overallString.trim()))
            return;
    } else {
        overallString = s.toString();
        startIndex = startIndex - 1;
    }

    int spanEnd = 0;
    for (int i = startIndex - 1; i &gt;= 0; i--) {
        Character c = overallString.charAt(i);
        if (c == ' ') {
            spanEnd = i;
            break;
        }
    }
    SpannableStringBuilder ssb = new SpannableStringBuilder(s);
    int cursorCurrentPoint = edt.getSelectionEnd();
    boolean addedFromFirst = cursorCurrentPoint &lt; overallString.length();

    ClickableSpan[] spans = ssb.getSpans(0, addedFromFirst ? spanEnd : ssb.length(), ClickableSpan.class);

    boolean someUnknownChange = false;
    if (spans.length &gt; 0) {
        ClickableSpan underlineSpan = spans[spans.length - 1];
        spanEnd = ssb.getSpanEnd(underlineSpan);
        // int spanCheck = spanEnd+1;

        int k = 0;
        for (int m = 0; m &lt; spans.length; m++) {
            ClickableSpan someSpan = spans[m];

            int end = ssb.getSpanEnd(someSpan);
            ;
            if (k &lt; end)
                k = end;
        }

        spanEnd = k;

        if (spanEnd &lt; overallString.length()) {
            Character c = overallString.charAt(spanEnd);
            if (c == ' ') {
                spanEnd += 1;
            } else {
                ssb.insert(spanEnd, " ");
                spanEnd += 1;
                startIndex += 1;
                someUnknownChange = true;
            }
        }
    }

    if (startIndex &gt; -1 &amp;&amp; spanEnd &gt; -1) {
        if (checkSpannable) {
            ClickableSpan[] span = someUnknownChange ? ssb.getSpans(spanEnd - 1, startIndex - 1, ClickableSpan.class) : ssb.getSpans(spanEnd, startIndex, ClickableSpan.class);
            if (span.length &gt; 0) {
                // has span
            } else if (startIndex &gt; spanEnd) {
                // ssb.replace(someUnknownChange?spanEnd-1:spanEnd,
                // someUnknownChange?startIndex-1:startIndex , "");
                ssb.replace(spanEnd, startIndex, "");

            }
            return;
        } else {
            // this is to checked whether the user deletes comma and adds
            // again
            if ((Math.abs(spanEnd - 1 - startIndex) &gt; 1)) {
                String userInputString = someUnknownChange ? overallString.substring(spanEnd - 1, startIndex - 1) : overallString.substring(spanEnd, startIndex);
                String trimString = userInputString.trim();
                if (trimString.length() == 0) {
                    ssb.replace(spanEnd, startIndex + 1, "");

                } else {
                    if (userInputString.charAt(userInputString.length() - 1) == ' ' &amp;&amp; spanEnd - 1 &gt;= 0 &amp;&amp; startIndex - 1 &gt;= 0)
                        userInputString = overallString.substring(spanEnd - 1, startIndex - 1);

                    if (checkValidation) {

                        BitmapDrawable bmpDrawable = getBitmapFromText(userInputString);
                        bmpDrawable.setBounds(0, 0, bmpDrawable.getIntrinsicWidth(), bmpDrawable.getIntrinsicHeight());
                        ssb.setSpan(new ImageSpan(bmpDrawable), spanEnd, startIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        // mm=phoneNum.getMovementMethod();
                        edt.setMovementMethod(LinkMovementMethod.getInstance());

                    } else {

                        BitmapDrawable bmpDrawable = getBitmapFromText(userInputString);
                        bmpDrawable.setBounds(0, 0, bmpDrawable.getIntrinsicWidth(), bmpDrawable.getIntrinsicHeight());
                        ssb.setSpan(new ImageSpan(bmpDrawable), spanEnd, startIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        // mm=phoneNum.getMovementMethod();
                        edt.setMovementMethod(LinkMovementMethod.getInstance());

                    }
                }
            }
        }
        setSpannableText(ssb);
    }
}

public void setSpannableText(final Spannable ssb) {
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {

            edt.setText(ssb);
            edt.setSelection(edt.getText().toString().length());
            resetFlags();
            if (!ssb.toString().equalsIgnoreCase(" ")) {
                str = ssb.toString();
            }
        }
    }, 20);
}

public void resetFlags() {
    isContactAddedFromDb = false;
    isTextAdditionInProgress = false;
    checkValidation = true;
}


private BitmapDrawable getBitmapFromText(String message) {
    TextView textView = (TextView) layoutInflater.inflate(R.layout.textview, null);
    textView.setText(message);
    BitmapDrawable bitmapDrawable = (BitmapDrawable) extractBitmapFromTextView(textView);
    return bitmapDrawable;
}

@SuppressWarnings("deprecation")
public static Object extractBitmapFromTextView(View view) {

    int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    view.measure(spec, spec);
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
    Bitmap b = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    c.translate(-view.getScrollX(), -view.getScrollY());
    view.draw(c);
    view.setDrawingCacheEnabled(true);
    Bitmap cacheBmp = view.getDrawingCache();
    Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
    view.destroyDrawingCache();
    return new BitmapDrawable(viewBmp);
}
</code></pre>