Android: Coloring part of a string using TextView.setText()?

Android: Coloring part of a string using TextView.setText()?

To color part of a string in a TextView using TextView.setText() in Android, you can use a SpannableString or SpannableStringBuilder. Here's how to do it:

Using SpannableString

TextView textView = findViewById(R.id.textView); // Create a SpannableString String fullText = "Hello, this is colored text!"; SpannableString spannableString = new SpannableString(fullText); // Set color for a specific part int start = 18; // Starting index int end = 29; // Ending index spannableString.setSpan(new ForegroundColorSpan(Color.RED), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // Set the spannable text to the TextView textView.setText(spannableString); 

Using SpannableStringBuilder

You can also use SpannableStringBuilder if you want to add multiple spans or styles.

TextView textView = findViewById(R.id.textView); // Create a SpannableStringBuilder SpannableStringBuilder spannableBuilder = new SpannableStringBuilder(); // Add text with different colors spannableBuilder.append("Hello, "); spannableBuilder.append("this is colored text!"); // Set color for a specific part spannableBuilder.setSpan(new ForegroundColorSpan(Color.RED), 7, 29, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // Set the spannable text to the TextView textView.setText(spannableBuilder); 

Summary

  1. Use SpannableString: For coloring part of a string.
  2. Use ForegroundColorSpan: To set the color.
  3. Set the result to TextView: Use setText() to apply.

This allows you to color specific parts of the text within a TextView.

Examples

  1. Android: Color part of a string using SpannableString Description: Use SpannableString to apply different styles (like color) to parts of a text displayed in a TextView.

    import android.graphics.Color; import android.os.Bundle; import android.text.SpannableString; import android.text.style.ForegroundColorSpan; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = findViewById(R.id.textView); String fullText = "Hello World"; SpannableString spannableString = new SpannableString(fullText); // Color part of the text int color = Color.RED; spannableString.setSpan(new ForegroundColorSpan(color), 6, 11, 0); textView.setText(spannableString); } } 
  2. Android: Coloring specific words in a string with multiple colors Description: Use SpannableString with multiple ForegroundColorSpan instances to color different parts of a text.

    import android.graphics.Color; import android.os.Bundle; import android.text.SpannableString; import android.text.style.ForegroundColorSpan; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = findViewById(R.id.textView); String fullText = "Hello Android World"; SpannableString spannableString = new SpannableString(fullText); // Color "Hello" in blue and "World" in green int colorBlue = Color.BLUE; int colorGreen = Color.GREEN; spannableString.setSpan(new ForegroundColorSpan(colorBlue), 0, 5, 0); spannableString.setSpan(new ForegroundColorSpan(colorGreen), 13, 18, 0); textView.setText(spannableString); } } 
  3. Android: Coloring part of a string using HTML tags with Html.fromHtml() Description: Use HTML tags to style parts of a string and display it in a TextView.

    import android.os.Bundle; import android.text.Html; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = findViewById(R.id.textView); String htmlText = "Hello <font color='red'>World</font>"; textView.setText(Html.fromHtml(htmlText)); } } 
  4. Android: Coloring substrings based on conditions with SpannableStringBuilder Description: Use SpannableStringBuilder to dynamically color parts of a text based on conditions.

    import android.graphics.Color; import android.os.Bundle; import android.text.SpannableStringBuilder; import android.text.style.ForegroundColorSpan; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = findViewById(R.id.textView); String fullText = "Android Development"; SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(fullText); // Conditionally color "Android" in blue and "Development" in green int colorBlue = Color.BLUE; int colorGreen = Color.GREEN; if (fullText.contains("Android")) { spannableStringBuilder.setSpan(new ForegroundColorSpan(colorBlue), 0, 7, 0); } if (fullText.contains("Development")) { spannableStringBuilder.setSpan(new ForegroundColorSpan(colorGreen), 8, 20, 0); } textView.setText(spannableStringBuilder); } } 
  5. Android: Coloring part of a string using TextUtils and SpannableString Description: Use TextUtils to find and color parts of a string using SpannableString.

    import android.graphics.Color; import android.os.Bundle; import android.text.SpannableString; import android.text.TextUtils; import android.text.style.ForegroundColorSpan; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = findViewById(R.id.textView); String fullText = "This is a sample text"; SpannableString spannableString = new SpannableString(fullText); int startIndex = TextUtils.indexOf(fullText, "sample"); int endIndex = startIndex + "sample".length(); spannableString.setSpan(new ForegroundColorSpan(Color.RED), startIndex, endIndex, 0); textView.setText(spannableString); } } 
  6. Android: Coloring part of a string using SpannableString and BackgroundColorSpan Description: Use BackgroundColorSpan to highlight parts of a string with different background colors.

    import android.graphics.Color; import android.os.Bundle; import android.text.SpannableString; import android.text.style.BackgroundColorSpan; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = findViewById(R.id.textView); String fullText = "This is a sample text"; SpannableString spannableString = new SpannableString(fullText); int startIndex = fullText.indexOf("sample"); int endIndex = startIndex + "sample".length(); spannableString.setSpan(new BackgroundColorSpan(Color.YELLOW), startIndex, endIndex, 0); textView.setText(spannableString); } } 
  7. Android: Coloring part of a string using SpannableString with clickable spans Description: Use clickable spans along with colored spans in SpannableString.

    import android.graphics.Color; import android.os.Bundle; import android.text.SpannableString; import android.text.style.ClickableSpan; import android.text.style.ForegroundColorSpan; import android.view.View; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = findViewById(R.id.textView); String fullText = "Click here for details"; SpannableString spannableString = new SpannableString(fullText); ClickableSpan clickableSpan = new ClickableSpan() { @Override public void onClick(View widget) { Toast.makeText(MainActivity.this, "Details clicked", Toast.LENGTH_SHORT).show(); } }; spannableString.setSpan(clickableSpan, 6, fullText.length(), 0); spannableString.setSpan(new ForegroundColorSpan(Color.BLUE), 6, fullText.length(), 0); textView.setText(spannableString); textView.setMovementMethod(android.text.method.LinkMovementMethod.getInstance()); } } 
  8. Android: Coloring part of a string using SpannableString with StyleSpan Description: Use StyleSpan to apply different styles (like bold) to parts of a text.

    import android.graphics.Typeface; import android.os.Bundle; import android.text.SpannableString; import android.text.style.StyleSpan; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = findViewById(R.id.textView); String fullText = "This is a sample text"; SpannableString spannableString = new SpannableString(fullText); int startIndex = fullText.indexOf("sample"); int endIndex = startIndex + "sample".length(); spannableString.setSpan(new StyleSpan(Typeface.BOLD), startIndex, endIndex, 0); textView.setText(spannableString); } } 
  9. Android: Coloring part of a string using SpannableStringBuilder with multiple styles Description: Use SpannableStringBuilder to apply multiple styles to different parts of a text.

    import android.graphics.Color; import android.os.Bundle; import android.text.SpannableStringBuilder; import android.text.style.ForegroundColorSpan; import android.text.style.StyleSpan; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = findViewById(R.id.textView); String fullText = "Bold Red Text"; SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(fullText); // Apply bold style to "Bold" and red color to "Red" spannableStringBuilder.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, 4, 0); spannableStringBuilder.setSpan(new ForegroundColorSpan(Color.RED), 5, 8, 0); textView.setText(spannableStringBuilder); } } 
  10. Android: Coloring part of a string using SpannableString with UnderlineSpan Description: Use UnderlineSpan to underline parts of a text.

    import android.os.Bundle; import android.text.SpannableString; import android.text.style.UnderlineSpan; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = findViewById(R.id.textView); String fullText = "Underline this"; SpannableString spannableString = new SpannableString(fullText); int startIndex = fullText.indexOf("Underline"); int endIndex = startIndex + "Underline".length(); spannableString.setSpan(new UnderlineSpan(), startIndex, endIndex, 0); textView.setText(spannableString); } } 

More Tags

parent-child ansible-2.x xsd.exe oh-my-zsh nfc qstackedwidget combinatorics asp.net-identity-2 tmux nswag

More Programming Questions

More Chemical reactions Calculators

More Mortgage and Real Estate Calculators

More Date and Time Calculators

More Livestock Calculators