The answer to this question works perfectly for my text... the first time. I have some resource file-defined strings:
<string name="permission_replace">PERMISSION</string>
<string name="warn_permission">This app needs PERMISSION to work properly. Update in <u>settings</u>.</string>
The <u>
tags work properly the first time I display the text, but when I go to replace my placeholder ("PERMISSION"
) the underline gets lost. Here's how I'm doing the replacement:
warnPermissionText.setText(warnPermissionText.getText().toString().replaceAll(getString(R.string.permission_replace),"some permission text"));
How can I retain the underline tags through the replacement? Or do I just have to programmatically add them back in? I'm not sure what magic does the formatting on the first go 'round.
Any time you call
toString()
, you are saying "hey, get rid of all that nice formatting that I had in there".The
TextUtils
class has a number of utility methods that can be applied directly to aCharSequence
, and therefore should retain formatting. In your case, thereplace()
method should work, at least for simple cases.In your specific case, you could also use the N-parameter form of
getString()
, if you changeThis app needs PERMISSION to work properly. Update in <u>settings</u>.
toThis app needs %s to work properly. Update in <u>settings</u>.
and supply the value for%s
in thegetString()
call.