Text view with different colored texts in xml code
android, colors, text, textview
Solution
There are three ways to change the color of some text inside a textview.
through `strings.xml` file in (res>values), using the tag (`<![CDATA[<p>This is green <font color='hexvalue of red'>and this is red</font>.</p> ]]>`) and then declaring the textview in java code as `myTextView.setText(Html.fromHtml(getString(R.string.myText));`
through java code, using the HTML tag `String text = "<font color='hexvalue of green'>This is green</font> <font color='hexvalue of red'>and this is red</font>."; myTextView.setText(Html.fromHtml((text));`
through `Spannable` text using java code.
`Spannable span` = `new SpannableString("My String");`
`span.setSpan(new ForegroundColorSpan(Color.RED), start_position,` `end_position,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);`
`myTextView.setText(span);`
If there are other ways to do it then I'm not aware of them. Hope this helps
Problem
I need my `textview` to have different colored texts. Also I need to do this from `xml` code, non from java code. Is there anyone who knows some way for doing this? Thanks e.g. I have sentence "This is red". I need words to be green, and word red to be red.