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 Button
programmatically.
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!
To achieve that you should set the drawable programatically.
I solved the problem with this line of code
Instead of
I get back the background of the my default
Button
withgetBackground()
and after i set the color withsetColorFilter(int, mode);
So the result become