How to add formatted text to text?

enter image description here

Image from an app called kakao story .

Suppose there is a comment list message, like any sns application.
When you click a comment, it inserts the commentator username into the edit text to indicate my new comment is a reply to the user .
(You cannot add the same name more than once.)
When you hit backspace to remove a name, all the characters that make up the name (e.g. chabeau in the example) will be deleted using 1-backspace.

I am trying to simulate a behavior and want some pointers on how to implement it or what to look for.

+6
source share
2 answers

If you are looking for a kind of bubble . You can achieve this by creating a subclass of android.text.style.DynamicDrawableSpan.ImageSpan that converts part of the EditText string to a formatted span .

This SO question will give you some general idea of ​​creating a formatted range.

This is a good guide to configure editext with spans .

And to delete the entire word at the same time, you can use the SPAN_EXCLUSIVE_EXCLUSIVE property.

Below the code will format the first four characters of the string. Hope this gives you some hint.

 final SpannableStringBuilder sb = new SpannableStringBuilder("your text here"); final ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(158, 158, 158)); // Span to set text color to some RGB value sb.setSpan(fcs, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); yourTextView.setText(sb); 
+2
source
 EditText et = (EditText) findViewById(R.id.edit1); et.setTextColor(Color.parseColor("yourColorCodeHere")); 
0
source

All Articles