Underlining text in Android that persists across a string replace

98 views Asked by At

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.

1

There are 1 answers

0
CommonsWare On BEST ANSWER

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 a CharSequence, and therefore should retain formatting. In your case, the replace() method should work, at least for simple cases.

In your specific case, you could also use the N-parameter form of getString(), if you change This app needs PERMISSION to work properly. Update in <u>settings</u>. to This app needs %s to work properly. Update in <u>settings</u>. and supply the value for %s in the getString() call.