How can I change my text color in app with button

Viewed 31

I want to create themes for my app so I am trying to add a button to change color. ColorDrawable works with background, but not for text color. What can I use for text colors.

A piece from my code:

ColorDrawable colorDrawableFont2 = (ColorDrawable) inputNoteSubtitle.getText();
        colorDrawableFont2.setColor(Color.parseColor(selectedNoteColorFont));
1 Answers

I assume you want to change color of your text and show it in your TextView

You can do it like this.

private fun correctText(text: String): Spannable {
        val word: Spannable = SpannableString(text)
        word.setSpan(
            ForegroundColorSpan(ContextCompat.getColor(this, R.color.cyanColor)),
            0,
            word.length,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        return word
    }

you can use this method to append text in your TextView with different color.

tvMyTextView.append(correctText("HELLO WORLD"))
Related