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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:24:19+00:00 2026-06-15T09:24:19+00:00

TextView tv = (TextView) findViewById(R.id.abc); String rawString = abcdefg; SpannableStringBuilder ssb = new SpannableStringBuilder(rawString);

  • 0
TextView tv = (TextView) findViewById(R.id.abc);
String rawString = "abcdefg";
SpannableStringBuilder ssb = new SpannableStringBuilder(rawString);
ssb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, rawString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(ssb);
CharSequence cs = tv.getText();
System.out.println(cs.getClass());

The output is “class android.text.SpannedString”

Why? I expect it to be “SpannableStringBuilder”, my casting (SpannableStringBuilder ssb_2 = (SpannableStringBuilder)cs;) will give error for the example above.

Some more, the output will change to “SpannableString” if I set the buffer type of the text view to be android:bufferType="spannable"

Anyone know why?

  • 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-15T09:24:20+00:00Added an answer on June 15, 2026 at 9:24 am

    1. Well the reason (SpannableStringBuilder ssb_2 = (SpannableStringBuilder)cs;) doesn’t work is because SpannableStringBuilder implements CharSequence, meaning CharSequence does not know it can be casted to that. You can however do it the other way around. Does that make sense?

    CharSequence is a nothing/itself

    SpannableStringBuilder is a: CharSequence, Spannable, Editable, etc…

    2. As for why android:bufferType="spannable" works, you are doing what I said above, the opposite. Since SpannableString implements CharSequence, it is now a child of it and therefore can be placed in CharSequence.

    But anyways, the correct way to put your CharSequence in SpannableStringBuilder is by doing:

    SpannableStringBuilder ssb_2 = SpannableStringBuilder(cs);
    

    You might want to brush up on some polymorphism 🙂 but you can see this in the Android docs on SpannableStringBuilder’s constructor, or one of them at least.


    Update:

    From what I notice on what you are doing, what is the need to use CharSequence? Just leave the TextView as is, meaning its returned as a String. So doing something like this would be easier:

    SpannableStringBuilder ssb_2 = SpannableStringBuilder(tv.getText());
    

    Reason that works is because String implements CharSequence as well, meaning that it as well can be passed into the SpannableStringBuilder constructor as a CharSequence. Java does automatic casting in certain cases, including in that code right above.

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

Sidebar

Related Questions

i am trying folling code into android tv=(TextView) findViewById(R.id.ringtone); if (!hasErrors) { setListAdapter(new ArrayAdapter<String>(FindFilesByType.this,
<TextView android:id=@+id/TextView03 android:layout_width=wrap_content android:layout_height=wrap_content android:layout_above=@+id/header android:layout_alignLeft=@+id/button1 android:layout_marginBottom=127dp android:text=@string/phone android:textColor=#000000 android:textSize=12dp android:typeface=sans /> I have
<TextView android:id=@+id/TextView03 android:layout_width=wrap_content android:layout_height=wrap_content android:layout_above=@+id/header android:layout_alignLeft=@+id/button1 android:layout_marginBottom=127dp android:text=@string/email android:textColor=#000000 android:textSize=12dp android:typeface=sans /> I have
This is java maintitle = (TextView)findViewById(R.id.text_particularlatestnewstitle); maintitle.setPadding(100, 0, 0, 0); alllatestnewslist.xml here <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android
TextView tv=(TextView)findViewById(R.id.textView1); Typeface face=Typeface.createFromAsset(getAssets(), font.ttf); tv.setTypeface(face); I am unable to see the textview typeface
final TextView dob = (TextView) findViewById(R.id.TextView_DOB); DatePickerDialog dateDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
int resID = getResources().getIdentifier(getResources().getString(R.string.hello_world), id, com.example.test.projects); TextView text = (TextView)findViewById(resID); text.setText(cpu: ); I cant
TextView t=(TextView)findViewById(R.id.yourtextview); String text = <fontcolor=#cc0029>Erste Farbe</font><font color=#ffcc00>zweiteFare</font>; yourtextview.setText(Html.fromHtml(text)); Getting NPE, I even added
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mTextView = (TextView) findViewById(R.id.text); new Thread(new
In my updateGUI() function I update each TextView like so: ((TextView) findViewById(R.id.lbTrackerLat)).setText(String.valueOf(lat)); ((TextView) findViewById(R.id.lbTrackerLong)).setText(String.valueOf(lng));

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.