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

  • Home
  • SEARCH
  • 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 7037331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:31:54+00:00 2026-05-28T01:31:54+00:00

I have the following input data in String: Hello #this# is #sample# text. It

  • 0

I have the following input data in String: “Hello #this# is #sample# text.”

It sets background color for all elements between # characters. Here’s what I got so far:

public static CharSequence colorBackground(CharSequence text) {

    Pattern pattern = Pattern.compile("#(.*?)#");

    Spannable spannable = new SpannableString( text );
    if( pattern != null )
    {
        Matcher matcher = pattern.matcher( text );
        while( matcher.find() )
        {
            int start = matcher.start();
            int end = matcher.end();
            CharacterStyle span = new BackgroundColorSpan(0xFF404040);
            spannable.setSpan( span, start, end, 0 );
        }
    }
    return spannable;
}

Setting background color works, but placeholder characters # are styled too. How to remove them before returning result, because method ReplaceAll doesn’t exist for CharSequence?

I use this function to style TextView rows in ListView. It feels a bit slower in emulator after adding this styling function. Maybe I should approach it in some other way e.g. use a custom TextView with custom draw function?

  • 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-05-28T01:31:55+00:00Added an answer on May 28, 2026 at 1:31 am

    This sounded like a fun thing to try to figure out.

    The key was SpannableStringBuilder. With SpannableString the text itself is immutable, but with SpannableStringBuilder both the text and the markup can be changed. With that in mind I modified your snippet a little to do what you want:

    public static CharSequence colorBackground(CharSequence text) {
    
        Pattern pattern = Pattern.compile("#(.*?)#");
    
        SpannableStringBuilder ssb = new SpannableStringBuilder( text );
    
        if( pattern != null )
        {
            Matcher matcher = pattern.matcher( text );
            int matchesSoFar = 0;
            while( matcher.find() )
            {
                int start = matcher.start() - (matchesSoFar * 2);
                int end = matcher.end() - (matchesSoFar * 2);
                CharacterStyle span = new BackgroundColorSpan(0xFF404040);
                ssb.setSpan( span, start + 1, end - 1, 0 );
                ssb.delete(start, start + 1);
                ssb.delete(end - 2, end -1);
                matchesSoFar++;
            }
        }
        return ssb;
    }
    

    I don’t have much experience with Spannables in general, and I don’t know if the way I’ve gone about deleting the “#” is the best method, but it seems to work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have the following code : <input type=text value=<?php echo $_GET['msg']; ?>> This input
I have the following input: <node TEXT=txt> <node TEXT=txt> <node TEXT=txt/> <node TEXT=txt/> </node>
I have the following text input on a budget calculator form which displays the
I have the following C# which simply replaces parts of the input string that
I have the following data stored in string ; string name = gokera atoram
I have the following JSON string coming from external input source: {value: "82363549923gnyh49c9djl239pjm01223", id:
I have the following data as input (sorted by first column): foo 1 2
I have a data input that looks like this: seq 75 T G -
Having the following hierarchical text data input (JunOS-like, in fact) I need to parse
I have a file input, in which i have the following data. 1 1Apple

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.