I have a view with a drawable set on it:
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/my_drawable" />
my_drawable
changes the way the View
looks when it is clicked/pressed, but does not set its background color. I want to set its background color in the code:
view.setBackgroundColor(myColor);
But this removes my_drawable
from it. How can I change the background color without removing my_drawable
?
EDIT
This is my_drawable
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@xml/button_round_corner_pressed_shape" android:state_pressed="true"></item>
<item android:drawable="@xml/button_round_corner_default_shape"></item>
</selector>
This is button_round_corner_pressed_shape
:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="@dimen/button_pressed_stroke_width"
android:color="@color/colorAccent" />
<corners android:radius="@dimen/button_corner_radius" />
</shape>
This finally worked for me:
<ImageView
android:id="@+id/view_background"
android:layout_width="@dimen/width"
android:layout_height="@dimen/height"
android:src="@xml/button_round_corner_default_shape" />
<View
android:layout_width="@dimen/width"
android:layout_height="@dimen/height"
android:background="@xml/my_drawable" />
And in the code:
viewBackground = findViewById<ImageView>(Resource.Id.view_background);
viewBackground.setColorFilter(color);
I combined pieces of everyone's answer to do this.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments