Essential Android TextView edit forum

Single TextView with two different colors

Colored text can be created by passing the text and a font color name to the following function:

private String getColoredSpanned(String text, String color) {
    String input = "<font color=" + color + ">" + text + "</font>";
    return input;
}

The colored text can then be set to a TextView (or even to a Button, EditText, etc.) by using the example code below.

First, define a TextView as follows:

TextView txtView = (TextView)findViewById(R.id.txtView);

Then, create differently colored text and assign it to strings:

String name = getColoredSpanned("Hiren", "#800000");
String surName = getColoredSpanned("Patel","#000080");

Finally, set the two differently colored strings to the TextView:

txtView.setText(Html.fromHtml(name+" "+surName));

Reference screenshot:


Table Of Contents
39 ACRA
64 Menu
109 TextView
112 Loader
119 Xposed
132 Colors
135 Fresco
140 AdMob
147 Button
156 Vk SDK
170 XMPP
176 OpenCV
200 FileIO
203 Moshi
217 Paint
231 AIDL
241 JCodec
243 Okio
255 Looper
  ↑ ↓ to navigate     ↵ to select     Esc to close