Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8038273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:10:59+00:00 2026-06-05T03:10:59+00:00

I have a requirement to build a textView which contains something like: Title[x] followed

  • 0

I have a requirement to build a textView which contains something like:
“Title[x]” followed by “some text” followed by “\n” followed by “Title[y]” followed by “more text” etc. giving

Title[x] some text

Title[y] more text

I need Title to be bold and to have a different color for each Title. I have 11 titles in total and up to 30 text strings to choose from and any title can have any text string. On each cycle, I will have from 1 to 6 Titles in my final resultText.
I have no problem building the textView with “known” values of x and y but, in real life, I won”t know the values until I come to build the spannable string. I don”t want to build every possible variation of strings (over 300).
I”ve tried creating all the “Title” spannables and appending them to my “result” as I create them and it works fine, but if I create them all and append them in one statement at the end I lose the Bold and Color attributes.

My main.xml has a textView with an ID of color_test.

package uk.cmj.color;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.View;
import android.widget.TextView;
import android.widget.TextView.BufferType;

public class ColorActivity extends Activity {
/** Called when the activity is first created. */

private static int titleIndex = 0;
private final String[] titles = {"Title A", "Title B", "Title C"};      

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// ---------------------------------------------
// THIS WORKS AND I SEE COLOR AND BOLD FOR TITLE
// ---------------------------------------------

titleIndex = 1;

SpannableStringBuilder resultText = new SpannableStringBuilder(); 
String firstTitle = titles[titleIndex]; 
SpannableString firstTitleSpannable= new SpannableString(firstTitle); 
firstTitleSpannable.setSpan(new ForegroundColorSpan(Color.rgb(155,79,25)), 0, firstTitle.length(), 0); 
firstTitleSpannable.setSpan(new StyleSpan(android.graphics.Typeface.BOLD),  0, firstTitle.length(), 0); 
resultText.append(firstTitleSpannable);

String body1 = " some text" + "\n"; 
SpannableString body1Spannable= new SpannableString(body1); 
body1Spannable.setSpan(new ForegroundColorSpan(Color.BLACK), 0, body1.length(), 0); 
resultText.append(body1Spannable); 

titleIndex = 2;

String nextTitle = titles[titleIndex]; 
SpannableString nextTitleSpannable= new SpannableString(nextTitle); 
nextTitleSpannable.setSpan(new ForegroundColorSpan(Color.rgb(155,79,25)), 0, nextTitle.length(), 0); 
nextTitleSpannable.setSpan(new StyleSpan(android.graphics.Typeface.BOLD),  0, nextTitle.length(), 0); 
resultText.append(nextTitleSpannable + "\n");

String body2 = " some different text" + "\n"; 
SpannableString body2Spannable= new SpannableString(body2); 
body1Spannable.setSpan(new ForegroundColorSpan(Color.BLACK), 0, body1.length(), 0); 
resultText.append(body2Spannable); 

TextView color_test = (TextView) findViewById(R.id.color_test); 
color_test.setText(resultText, BufferType.SPANNABLE); 

// ------------------------------------------
// THIS DOESN'T WORK AS I LOSE COLOR AND BOLD
// ------------------------------------------

titleIndex = 1;

SpannableStringBuilder resultText = new SpannableStringBuilder(); 
String firstTitle = titles[titleIndex]; 
SpannableString firstTitleSpannable= new SpannableString(firstTitle); 
firstTitleSpannable.setSpan(new ForegroundColorSpan(Color.rgb(155,79,25)), 0, firstTitle.length(), 0); 
firstTitleSpannable.setSpan(new StyleSpan(android.graphics.Typeface.BOLD),  0, firstTitle.length(), 0); 

String body1 = " some text" + "\n"; 
SpannableString body1Spannable= new SpannableString(body1); 
body1Spannable.setSpan(new ForegroundColorSpan(Color.BLACK), 0, body1.length(), 0); 
resultText.append(body1Spannable); 

titleIndex = 2;

String nextTitle = titles[titleIndex]; 
SpannableString nextTitleSpannable= new SpannableString(nextTitle); 
nextTitleSpannable.setSpan(new ForegroundColorSpan(Color.rgb(155,79,25)), 0, nextTitle.length(), 0); 
nextTitleSpannable.setSpan(new StyleSpan(android.graphics.Typeface.BOLD),  0, nextTitle.length(), 0); 

String body2 = " some different text" + "\n"; 
SpannableString body2Spannable= new SpannableString(body2); 
body1Spannable.setSpan(new ForegroundColorSpan(Color.BLACK), 0, body1.length(), 0); 
resultText.append(body2Spannable); 

resultText.append(firstTitleSpannable 
                + body1Spannable 
                + nextTitleSpannable
                + body2Spannable); 


TextView color_test = (TextView) findViewById(R.id.color_test); 
color_test.setText(resultText, BufferType.SPANNABLE); 

}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T03:11:01+00:00Added an answer on June 5, 2026 at 3:11 am

    I’m not quite sure if I understand your problem correctly. But if I do, I’d write a class which contains all the attributes needed and create objects during runtime:

    public class SpannableContent {
        private StringBuilder result = new StringBuilder();
        private String title1 = null;
        private String title2 = null;
        private String description = null;
    
        public SpannableContent(String _title1, String _title2, String _desc) {
            this.title1 = _title1;
            this.title2 = _title2;
            this.description = _desc;
    
            result.append(getFormatedTitle1());
            result.append(getFormatedTitle2());
            result.append(getFormatedDescription());
        }
    
        private String getFormatedTitle1() {
            String resString = null;
            // do whatever Spannable stuff you want to do w/ title1 here
    
            return resString;
        }
    
        private String getFormatedTitle2() {
            String resString = null;
            // do whatever Spannable stuff you want to do w/ title2 here
    
            return resString;
        }
    
        private String getFormatedDescription() {
            String resString = null;
            // do whatever Spannable stuff you want to do w/ the description here
    
            return resString;
        }
    
        public String getFinalContent() {
            return result.toString();
        }
    }
    

    Then do during runtime something like

    SpannableContent spannableC = new SpannableContent(dynamicTitle1, dynamicTitle2, dynamicDesc);
    resultText.append(spannableC.getFinalContent());
    

    You could store the objects in an ArrayList of the type SpannableContent.

    ArrayList<SpannableContent> spannableArrayList = new ArrayList<SpannableContent>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have requirement to build new rule engine in which i have rule like
I have requirement where some times I would like to load children as well
I have requirement like, suppose I have a 'property' table which has 'ListingKey' field
I have the requirement to build a asp.net sign up form which will allow
I have a requirement to build a .net 3.5 webpage for a client which
I have a requirement to build a flex mobile app (iOS/Android) which is capable
I have a requirement. I need to build an app, which uses Bluetooth to
We have a requirement to build a small Sinatra app which will capture events
i have a requirement to build some sort of services that can easily be
I have a requirement for my MAVEN build to pick a single jar from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.