How to change background Color to MaterialButton from android.support.design programmatically

I found the answer which is as below:

I changed the following line

materialButton.setBackgroundColor( ContextCompat.getColor(this@MyActivity, R.color.myCustomColor));

to

materialButton.setBackgroundTintList(ContextCompat.getColorStateList(this@MyActivity, R.color.myCustomColor));

I tested it that way and it worked perfectly.


Simply call this attribute in the XML file within Material Button.

 android:backgroundTint=""

Lets try this:

mButton.getBackground().setColorFilter(getResources().getColor(R.color.indian_red), PorterDuff.Mode.SRC_ATOP);