Search code examples
androidstringtextcolorstextcolor

I need to change the color of only a file from my string


I have the following string:

<string name="welcome.location.services">Please <font color='green'>ALLOW</font> Passenger to access your location in order to track your trips.</string>

If I leave it like this, it will colour ALLOW with the default green. But I need a different colour there. How can I change it? Tried:

<font color='#ff00ff'>ALLOW</font>

But the word will have no colour like this. I want to do it in the strings.xml cause I have more files for each language, and the words length and start index is different, making spannable strings hard to work with

PS: I tried using HTML.fromHTML but still no luck:

((TextView)dialog.findViewById(R.id.header4)).setText(Html.fromHtml(context.getString(R.string.welcome_location_services)));

Where the string is:

<string name="welcome.location.services">Please <font color='#ffff00d3'>ALLOW</font> Passenger to access your location in order to track your trips.</string>

Solution

  • try this, and let me know.

    String s = "Hello World, I'm the rest.";
     String[] result = s.split(" ", 2);
     String first = result[0];
     String rest = result[1];
    
     first = "<font color='#EE0000'>red</font>";
     t.setText(Html.fromHtml(first + rest));