String mySimpleWinString mySimpleWinString

String mysimplewinstring mysimplewinstring

This preview shows page 37 - 42 out of 112 pages.

String mySimpleWinString; mySimpleWinString = getResources().getString(R.string.winLose); String escapedWin = TextUtils.htmlEncode(mySimpleWinString); String resultText = String.format(mySimpleWinString, 5, 5, escapedWin); The resulting text in the resultText variable is: Score: 5 of 5! You Won
Image of page 37

Subscribe to view the full document.

Using String Resources Programmatically (Cont’d) If you have styling in this format string like the preceding string resource winLoseStyled , you need to take a few more steps to handle the escaped italic tags. For this, you might want to use the fromHtml() method of the Html class ( android.text.Html ), as shown here. This variable, styledResults , can then be used in user interface controls such as TextView objects, where styled text is displayed correctly. String myStyledWinString; myStyledWinString = getResources().getString(R.string.winLoseStyled); String escapedWin = TextUtils.htmlEncode(myStyledWinString); String resultText = String.format(myStyledWinString, 5, 5, escapedWin); CharSequence styledResults = Html.fromHtml(resultText); The resulting text in the styledResults variable is: Score: 5 of 5! You <i>Won</i>
Image of page 38