3

I have this String:

    String[] text = {"Address 1: Street nr.45 ",
         "Address 2: Street nr.67",
         "Address 3: Street nr. 56 \n Phone number: 000000000"};

Which gets used later by:

        ((TextView)findViewById(R.id.adresa)).setText(text[newSelectedAddress]);

when selecting an item from a spinner.

How can I add formatting to the text inside the string?? I want Address to be with bold, Street nr. with Italic

Catalin H
  • 215
  • 4
  • 14

3 Answers3

3

You have to use Spannables. Although creating them is a bit wordy and seems complex, it isn't rocket science. An example:

String source = "This is example text";
SpannedString out = new SpannedString(source);
StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
StyleSpan boldSpan2 = new StyleSpan(Typeface.BOLD);
out.setSpan(boldSpan, 1, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
out.setSpan(boldSpan2, 9, 12, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

Result: This is example text

You then setText this Spannable instead of the normal String.

poitroae
  • 21,129
  • 10
  • 63
  • 81
  • Well thing is that on that String text I will have 30 more texts. If I do it in this it will take way too much. – Catalin H Feb 10 '13 at 13:04
  • @CatalinH Just create a method which takes the text to format, format parameters (bold etc) and the start index and length of the text to format. Call that method wherever you need to. Absolutely no need to repeat 30 times. – Simon Feb 10 '13 at 13:30
  • I get the error:Type Mismatch: Cannot convert from SpannedString to Spannable – Catalin H Feb 10 '13 at 14:16
1

Do Like this my dear friend:

String[] text = {"Address 1: Street nr.45 ",
             "Address 2: Street nr.67",
             "Address 3: Street nr. 56 \n Phone number: 000000000"};
    String tempString = text[newSelectedAddress];
    CharSequence charSeq= new SpannedString(tempString);
    Spannable spannable = (Spannable) charSeq;
    StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
    StyleSpan boldSpan2 = new StyleSpan(Typeface.ITALIC);
    spannable.setSpan(boldSpan, 0, "Address".length()-1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    spannable.setSpan(boldSpan2, tempString.indexOf("Street"), tempString.indexOf("Street")-1 +"Street".length()-1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    ((TextView)findViewById(R.id.adresa)).setText(spannable);

Thanks

Shridutt Kothari
  • 7,326
  • 3
  • 41
  • 61
  • 02-10 16:09:06.800: E/AndroidRuntime(612): java.lang.ClassCastException: android.text.SpannedString cannot be cast to android.text.Spannable – Catalin H Feb 10 '13 at 14:14
0

There's a bit of a hack to allow strings to recognize html for bold and italic formatting. Add something like this to your strings.xml:

<string name="address"><![CDATA[<b>Address %1$s</b>: <i>Street nr. %2$s </i>]]></string>

Then:

Resources res = getResources();
formattedString = res.getString(R.string.address, "1", "45");
// equivalent to String.format(res.getString(R.string.address), "1", "45")
Spannable s = Html.from Html(formattedString);
textView.setText(s);
vinc3m1
  • 4,075
  • 1
  • 26
  • 23