Android how to programmatically set Button stroke

2019-09-11 19:37发布

问题:

I have this activity with 48 Button that the user can touch and change text and background color.

I have edited the style of the default Buttons like this

But when the user change the background color i have this bad result

These are the xmls that style the dafault Buttons

buttons.xml

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

    <item android:drawable="@drawable/button_pressed"
        android:state_pressed="true" />

    <item android:drawable="@drawable/button_focused"
        android:state_focused="true" />

    <item android:drawable="@drawable/button_default" />

</selector>

button_default.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <corners
        android:radius="100dp"
        />
    <solid
        android:color="#FFFFFF"
        />
    <padding
        android:left="0dp"
        android:top="0dp"
        android:right="0dp"
        android:bottom="0dp"
        />
    <stroke
        android:width="3dp"
        android:color="#787878"
        />
</shape>

In the other xml change only the color so i avoid to post them. And here is the code that change the Buttonprogrammatically. I take from DB all changed Button saved with ID and set the color of the Button.

   //Get all materie inside database
    List<Materia> materia = db.getAllMaterie();
    //change all TextView inputed from user
    if(materia.isEmpty()){
        //do nothing
    }else {
        for (Materia mat : materia) {
            //Change all the Button with values stored inside the database
            int resId = getResources().getIdentifier(mat.getID(), "id", getPackageName());
            final Button changedButton = (Button) findViewById(resId);
            changedButton.setText(mat.getMateria());
            changedButton.setTypeface(null, Typeface.BOLD);
            changedButton.setBackgroundColor(mat.getColor());

        }
    }

But i lose the radius and stroke property. There is some way to set them programmatically? Other suggestions are accepted!

回答1:

To achieve that you should set the drawable programatically.

Drawable buttonDrawable = context.getResources().getDrawable(R.drawable.buttons.xml);
buttonDrawable.mutate()
changedButton.setBackgroundDrawable(buttonDrawable);


回答2:

I solved the problem with this line of code

changedButton.getBackground().setColorFilter(mat.getColor(), PorterDuff.Mode.MULTIPLY);

Instead of

changedButton.setBackgroundColor(mat.getColor());

I get back the background of the my default Button with getBackground() and after i set the color with setColorFilter(int, mode);

So the result become

//Get all materie inside database
    List<Materia> materia = db.getAllMaterie();
    //change all TextView inputed from user
    if(materia.isEmpty()){
        //do nothing
    }else {
        for (Materia mat : materia) {
            //Change all the Button with values stored inside the database
            int resId = getResources().getIdentifier(mat.getID(), "id", getPackageName());
            final Button changedButton = (Button) findViewById(resId);
            changedButton.setText(mat.getMateria());
            changedButton.setTypeface(null, Typeface.BOLD);
            changedButton.getBackground().setColorFilter(mat.getColor(), PorterDuff.Mode.MULTIPLY);

        }
    }