Change color of a Button that already has an Assigned Background

2

Hello, in my application there is a table (Alert Dialog) that will be used to select the color of a component. In this table there are several equal buttons with the background already defined so that they have a round style. For this, I used an XML file with the shape property.

I wanted each of these buttons to have a different color. However, I do not want to create multiple XML files by setting the color of each. I want to know if you have any different way of changing the color without touching your background, or even changing the color of the shape button.

I also accept suggestions for doing this table in a different way, without needing to use a lot of buttons.

Button background XML file:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <solid
        android:color="@color/colorPrimary"
        />

    <stroke
        android:width="2dp"
        android:color="@color/colorPrimary" />
</shape>
    
asked by anonymous 02.11.2017 / 17:47

1 answer

1

If minSdkVersion > = 21 use

android:backgroundTint

For lower versions, change the color from solid to #FFFFFF and use the following code to put the color you want,

button = (Button)findViewById(R.id.button);
Drawable background = button.getBackground();
background.setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY); 

If you want an alternative to a "full-button" layout, see this response . In it a Spinner is used to choose the color, but the adapter can be used in a ListView.

    
02.11.2017 / 19:08