Is it possible to apply multiple styles in a text view?

Asked 1 years ago, Updated 1 years ago, 103 views

For example, tv.setText(line1 + "\n" + line2 + "\n" + word1 + "\t" + word2 + "\t" + word3); If there is a sentence like this, I would like to apply different styles for each line1, line2, word1, word2 The sauce I found is

 // Get Edit Text.
    EditText vw = (EditText)findViewById(R.id.text);

    // Enter text in EditText
    vw.setText("Italic, highlighted, bold.");

    Spannable str = vw.getText();

    str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    str.setSpan(new BackgroundColorSpan(0xFFFFFF00), 8, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 21, str.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

This is how you write down the index and the number for each text. Is there a cleaner way?

android textview style

2022-09-22 22:34

1 Answers

Change to HTML

mBox = new TextView(context);
mBox.setText(Html.fromHtml("<b>" + title + "</b>" +  "<br />" + 
            "<small>" + description + "</small>" + "<br />" + 
            "<small>" + DateAdded + "</small>"));

There's a way.


2022-09-22 22:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.