0

I am making an android advanced editor that allow user to change text color, styles (bold, italic, underline) while typing.

I'm able to achieve the styles by using SpannableStringBuilder, but seemlike SpannableStringBuilder can be applied to existing characters only (like highlight them and change color).

How do I achieve the same affect when the user typing into new characters without base on existing text?

For example, If i choose yellow text color and underline on my controls, when the user type new characters, these characterse will be underline and be yellow.

Huy.Vu
  • 173
  • 10

1 Answers1

0

Just add modify edittext in beforeTextChange fun

 binding.edtCustomInput.doBeforeTextChanged { text, start, count, after ->
        //set style here
        binding.edtCustomInput.setTextColor(Color.YELLOW)
        binding.edtCustomInput.paintFlags = Paint.UNDERLINE_TEXT_FLAG
    }
Anh Luu
  • 176
  • 1
  • 7
  • That's not correct, i want only a part of the text to be changed, not the entire text – Huy.Vu Dec 28 '21 at 14:44
  • So weird a require is @@ Just a solution, create a linearlayout with horizontal orientation, everytime you take new color, add a new edittext by addView() with specific color. – Anh Luu Dec 29 '21 at 02:05
  • I have found the most correct solution to do this, which is using SpannableStringBuilder with EditText.TextFormatted, you can do any custom styles just a part of the text and while typing as well. – Huy.Vu Dec 29 '21 at 13:13