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 9231267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:01:33+00:00 2026-06-18T06:01:33+00:00

I’m trying to get some code which will for example change ^^text^^ Into something

  • 0

I’m trying to get some code which will for example change

“^^text^^”

Into something like this

“text“

So I need it to remove the ^^ tokens and add a Spannable Text on the “text” string making it bold.
Right now I have this code.

public void format()
{
    TextView textView = (TextView) findViewById(R.id.test);

    CharSequence text = null;

    if(textView != null)
    {
         text = textView.getText();
    }

    String token = "^^";

    if(text != null)
    {
        int length = text.length();
        int start = text.toString().indexOf(token) + length;
        int end = text.toString().indexOf(token, start);

        if (start > -1 && end > -1)
        {
            SpannableStringBuilder ssb = new SpannableStringBuilder(text);

            ssb.setSpan(new ForegroundColorSpan(0x000000), start, end, 0);
            ssb.delete(end, end + length);
            ssb.delete(start - length, start);

            text = ssb;
            System.out.println("format");
        }

        System.out.println("works");
    }

    textView.setText(text);
    System.out.println("running");
}

This code gets called in the onCreate which looks like this.

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    format();

    setContentView(R.layout.activity_tutorial_basic_file_test);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
    {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

This is the XML layout file I’m using.

<LinearLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:text="@string/empty" />

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="2dp"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp"
    android:layout_marginTop="2dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/test"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/test_text"
            android:textColor="#FFFFFF" />
    </LinearLayout>
</ScrollView>

And finally the string I’m using in a resource file.

<string name="test_text">Test string ^^ for ^^ formatter code</string>

What am I doing wrong or how should I do this?

  • 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-18T06:01:34+00:00Added an answer on June 18, 2026 at 6:01 am

    You have several problems here:

    1. In onCreate you call format() before setting the content layout. First set layout, then operate on views that are declared inside it, i.e.:

      // First!
      setContentView(R.layout.activity_tutorial_basic_file_test);
      // Second!
      format();
      
    2. In format you mistakenly define length as length of the text. Looking at the code, it should definitely be length of token:

      // token length, not text length!
      int length = token.length();
      
    3. Remember that providing color values as int values often means you should explicitly set alpha value. If you don’t the alpha is set to 0 (i.e. transparent). You use 0x000000 (transparent black) in ForegroundColorSpan, but it’s more common to set 0xff000000 (opaque black). If that’s the case, change the line to:

      ssb.setSpan(new ForegroundColorSpan(0xff000000), start, end, 0);
      
    4. You should set the TextView content to the SpannableStringBuilder directly. This is because TextView automatically sees that it is a Spannable and applies your desired formatting. Whereas setting it to a string representation of the SpannableStringBuilder doesn’t apply formatting at all. So you should replace the line:

      text = ssb;
      

      with:

      textView.setText(sbb);
      

      and delete the latter:

      textView.setText(text);
      

    The final code is as follows:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_format_test);
        format();
        // Your custom code was here, deleted for readability
    }
    
    public void format() {
        TextView textView = (TextView)findViewById(R.id.test);
    
        CharSequence text = null;
    
        if (textView != null) {
            text = textView.getText();
        }
    
        String token = "^^";
    
        if (text != null) {
            int length = token.length();
            int start = text.toString().indexOf(token) + length;
            int end = text.toString().indexOf(token, start + length);
    
            if (start > -1 && end > -1) {
                SpannableStringBuilder ssb = new SpannableStringBuilder(text);
                ssb.setSpan(new ForegroundColorSpan(0xff000000), start, end, 0);
                ssb.delete(end, end + length);
                ssb.delete(start - length, start);
    
                textView.setText(ssb);
                System.out.println("format");
            }
    
            System.out.println("works");
        }
    
        System.out.println("running");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.