I Want to add amount in String "confirm_msg"
final int colorGreen = getResources().getColor(R.color.green_dark);
SpannableString spannable = new SpannableString("$" +amount);
spannable.setSpan(new ForegroundColorSpan(colorGreen), 0, ("$" + amount).length(), 0);
txtConfirmMSG.setText(getString(R.string.confirm_msg, spannable));
this is string in xml:
<string name="confirm_msg">PLEASE CONFIRM THAT YOU WISH TO SEND A %1$s </string>
// try this way
<string name="confirm_msg">PLEASE CONFIRM THAT YOU WISH TO SEND A %1$d$ </string>
int amount = 150000;
txtConfirmMSG.setText(setSpanColor(String.format(getString(R.string.confirm_msg),amount),String.valueOf(amount)));
private SpannableStringBuilder setSpanColor(
final String str,final String text) {
SpannableStringBuilder ssb = new SpannableStringBuilder(str);
if (str.contains(text)) {
ssb.setSpan(new ClickableSpan() {
@Override
public void onClick(View view) {
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setColor(getResources().getColor(R.color.green_dark));
}
}, str.indexOf(text), str.indexOf(text)
+ text.length(), 0);
}
return ssb;
}
It's best to use HTML to achieve different colors in Spannable
.
You can check that answer: Highlighting Text Color using Html.fromHtml() in Android?