0

My Android app is displaying text in a TextView.

Are there any tags or anything to put around words that I want italicized? I don't need to set the TextView as italics because the whole sentence would be that way, and I only need specific words italicized.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
Mark
  • 1,130
  • 3
  • 17
  • 32

4 Answers4

1

You need to use a Spannable: see Is there any example about Spanned and Spannable text for an example.

Community
  • 1
  • 1
Femi
  • 64,273
  • 8
  • 118
  • 148
0

This function sets a string as the text of a TextView and italicizes one or more spans within that string as long as the span or spans are marked with the tags [i] and [/i]. Of course, it can be modified for other types of styling.

private void doItalicize(TextView xTextView, String xString) {
        ArrayList<Integer> IndexStart = new ArrayList<>();
        ArrayList<Integer> IndexEnd = new ArrayList<>();
        ArrayList<StyleSpan> SpanArray = new ArrayList<>();

        int i = 0;

        do {
            IndexStart.add(i, xString.indexOf("[i]"));
            IndexEnd.add(i, xString.indexOf("[/i]") - 3);
            xString = xString.replaceFirst("\\[i\\]", "");
            xString = xString.replaceFirst("\\[/i\\]", "");
            xTextView.setText(xString, TextView.BufferType.SPANNABLE);
            SpanArray.add(i, new StyleSpan(Typeface.ITALIC));
            Log.d(LOG_TAG, "i: " + i);
            i++;
        } while (xString.contains("[i]"));

        Spannable xSpannable = (Spannable) xTextView.getText();

        for (int j = 0; j < i; j++)
            xSpannable.setSpan(SpanArray.get(j), IndexStart.get(j), IndexEnd.get(j), Spanned
                    .SPAN_EXCLUSIVE_EXCLUSIVE);
    }
0

I agree, this isn't a database issue. If this is not a custom app you're working on, you're out of luck. If it is, save the field in the database as HTML.

Stan C
  • 271
  • 1
  • 6
  • 14
-1

The solution would seem to be to save the content in the database as HTML and then output the HTML in the textview.

See the following article: How to display HTML in TextView?

Community
  • 1
  • 1
Tim Almond
  • 12,088
  • 10
  • 40
  • 50