dbenitobaldeon
dbenitobaldeon

Reputation: 324

Change color to a specific text in a string

I have this string where I use a spam class giving color to a text, but I do not get results, I have researched but nothing gives me a solution

    <string name="successfull_registration">
    Please, use the means of 
    payment of the Credit Bank that most suits you. Box Office, 
    Transfer, Cashier or BCP Agent, to make  payment to account
    <span class="blue">0102-0228345543469</span> Pympack SAC \n
    </string>

Here I use the spam class

    <span class="blue">0102-0228345543469</span>

Upvotes: 0

Views: 65

Answers (2)

Diyako
Diyako

Reputation: 671

if you want to use span, you can configure in java :

        SpannableString Spann = new SpannableString ( "your text source" );
        ClickableSpan CSpan = new ClickableSpan (){
            @Override
            public void onClick(View v){
    Toast.makeText(getApplicationContext() , "your clickable",
      Toast.LENGTH_LONG).show();
            }
        };
        
        Spann.setSpan(backgroundSpan, 0, Spann.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        yourTextViewId.setText(Spann);
        yourTextViewId.setMovementMethod(LinkMovementMethod.getInstance());

Upvotes: 2

tgrable
tgrable

Reputation: 1053

Use SpannableString instead.

val string = SpannableString(resources.getString(R.string.successfull_registration))
string.setSpan(
    ForegroundColorSpan(Color.BLUE),
    144,
    162,
    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
  )

textView.text = string

Upvotes: 2

Related Questions