Android EditText, change color, styles while typing

201 Views Asked by At

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.

1

There are 1 best solutions below

3
Anh Luu On

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
    }