'Android Studio change button background color with custom drawable

I have a custom drawable using <shape> in xml, with the following code:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/blue"/>
    <corners
        android:radius="150dp"/>
</shape>

on a button, and I would like to programmatically change the button's color to another color while keeping the shape it gets from the custom drawable. Is this possible?



Solution 1:[1]

If you dont want to use the normal backgrounTint in your xml folder :

    android:backgroundTint="yourcolor"

Use this Code in your java code :

    yourbutton.setBackgroundTintList(this.getResources().getColorStateList(R.color.colorname));
    

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Moha_E