Search code examples
javaandroidandroid-layouttextview

Set text color to secondary color programmatically


How can I set the color of my text to "textColorSecondary" programmatically? I've tried the code below but it doesn't work. Does anyone know what's wrong with the code?

TextView tv1 = ((TextView)v.findViewById(R.id.hello_world));
tv1.setTextColor(Color.textColorSecondary);

Solution

  • First add textColorSecondary in your colors.xml

    <color name="textColorSecondary">PutColorCodeHere</color>
    

    And then set color in code :

    tv1.setTextColor(getResource.getColor(R.color.textColorSecondary));