How to change Android Button Color using xml attribute and programatically using java

By default we have a grayish looking Button view for Android if you want to change the color you can do it either using XML attribute for tag or programmatically using java code, let's see both of them,

>Change Button color using XML attribute

All we need to do is add android:background attribute to the view xml tag, we can set colors as hex codes with opacity. We can also set image as background for a button using android:background:"@drawable/imageName attribute, lets see some examples,

Change Button color using xml attribute

button.setBackgroundResource(R.drawable.image_name);
button.setBackgroundResource(Color.RED);
button.setBackgroundResource(Color.rgb(100, 100, 100));
Popular Tag List Author Info:

Rakesh (He/Him) has a Masters Degree in Computer Science with over 15+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

You can also reach out to him via e-mail: rakesh@code2care.org