Reputation: 2703
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>
Upvotes: 2
Views: 1118
Reputation: 2703
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.
Upvotes: 0
Reputation: 408
But this removes
my_drawable
from it. How can I change the background color without removingmy_drawable
?
You want to change the background color by not removing the my_drawable? That seems confusing my friend. Why would you change the background color if you don't want to remove the drawable?
Which background color do you really want to be changed?
You can also just change the drawable to a new one..
The property android:background
actually accepts a few types. You can try a drawable and a color.
You can also programmatically change this by using view.setBackgroundColor(color)
for color, and view.setBackground(drawable)
for background drawable.
References
Upvotes: 0
Reputation: 6857
You can use ImageView and set your drawable as below code:
<ImageView
android:id="@+id/view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/green_circle" />
Then you can change color of your drawable at run time without changing your drawable:
ImageView view=(ImageView)findViewById(R.id.view);
view.setColorFilter(Color.RED);
Upvotes: 1
Reputation: 21043
Use CheckedText view with selector for checked , unchecked stated . Onclick just toggle the checked state .
checkedTxt.setChecked(true).
Upvotes: 0
Reputation: 925
One solution is to overlap the view with an ImageView so that you can set the background colour in the view and set the drawable in the ImageView.
Upvotes: 1
Reputation: 18276
Use a ImageView with the drawable as android:src and change the background as you wish.
Upvotes: 1