I have created a button with border:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#FFFFFFFF" />
<stroke
android:width="1dp"
android:color="#FFCCCCCC" />
</shape>
and
<Button
android:text="@null"
android:stateListAnimator="@null"
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/colorButton"
android:background="@drawable/button_border" />
Now I change the color of the background programmatically. The problem is that the border gets removed as soon as I change the background. Is there a way to change to background color of the button and keeping the border?
Try this,
Button colorButton = (Button) findViewById(R.id.colorButton);
GradientDrawable background = (GradientDrawable) colorButton.getBackground();
background.setColor(getResources().getColor(R.color.some_color));
Use following code
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#FFFFFFFF" />
<stroke
android:width="1dp"
android:color="#FFCCCCCC" />
<gradient
android:startColor="@color/white"
android:centerColor="@color/white"
android:endColor="@color/white"/>
</shape>
For Changing color dynamically use following code.
Drawable background = yourView.getBackground();
if (background instanceof ShapeDrawable) {
// cast to 'ShapeDrawable'
ShapeDrawable shapeDrawable = (ShapeDrawable) background;
shapeDrawable.getPaint().setColor(getResources().getColor(R.color.colorToSet));
} else if (background instanceof GradientDrawable) {
// cast to 'GradientDrawable'
GradientDrawable gradientDrawable = (GradientDrawable) background;
gradientDrawable.setColor(getResources().getColor(R.color.colorToSet));
} else if (background instanceof ColorDrawable) {
// alpha value may need to be set again after this call
ColorDrawable colorDrawable = (ColorDrawable) background;
colorDrawable.setColor(getResources().getColor(R.color.colorToSet));
}
From my point of view no need to create cutom drawable just add any layout Apply margin android:layout_margin="1dp"
to your button and android:background="#FF4081"
for RelativeLayout.Now just change the background of your buttton.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FF4081">
<Button
android:id="@+id/colorButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="1dp"
android:background="@android:color/white"
android:text="@null" />
</RelativeLayout>
To apply new color on same drawable use DrawableCompat class.
DrawableCompat.setTintList(d,drawableTintColor); // d is drawable object and drawableTintColor is color you want to apply
Maybe you should try and get a reference to the background drawable and then apply some color, like the code I post below :
GradientDrawable gradientDrawable = (GradientDrawable) colorButton.getBackground();
gradientDrawable.setColorFilter(Color.parseColor("#FF0000"), PorterDuff.Mode.SRC);
where #FF0000
is the new color you want to display.
That way I think borders will not get removed .