Set button text color programmatically

1

I have a button created in my class Main via code, that is, programmatically. See:

Button btnJonSnow = new Button(this);
btnJonSnow.setText("Jon Snow");

How can I set the color of the button text programmatically?

    
asked by anonymous 03.04.2017 / 07:48

1 answer

2

To set a color for the button text, you must use the setTextColor() attribute. Soon to redeem the color you can use the static method getColor() of the public class ContextCompat . Here's an example:

Button btnJonSnow = new Button(this);
btnJonSnow.setText("Jon Snow");
btnJonSnow.setTextColor(ContextCompat.getColor(mContext, R.color.colorAccent));
    
03.04.2017 / 07:55