Change background color of a view without removing drawable

Darius

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>
Darius

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.

edited at
0

Comments

0 comments
Login to comment

Related

How to change the background color of a QScrollArea without affecting the scrollbar color?

How to change background/color of the drawable set as background of a view?

Change drawable color programmatically

Animate change of view background color on Android

How to change selector background color without filling options background colors?

Change Recycler view item background color by code

How to change background color of drawable shape in custom adapter

Change the color balance of a Drawable

FAB Change color of icon drawable

Unable to change background color of view in SwiftUI

Change background image but keep drawable to a view android xml

How to change the background color of a View Drawable without changing its body in recyclerView?

change EditText's background color without clearing the border color

How to change AlertDialog view background color?

MKPolyLineRenderer change color without removing overlay

Change color of drawable xml files progmatically without findViewById()

change background color of view by its ID in Titanium

How to change color of drawable set as android:background?

Change element's background color with gradient to background color without gradient

Change Action Bar color to background color of recycler view item

How to get and change drawable background color in custom view

How to use animatorSet to change background color of a View

change color of @drawable inside of an item

How to change background color using color picker without click on button?

change color of drawable in a button Android

Android Studio change button background color with custom drawable

Change the background color of a Navigation View in Swift 5

Background color not change TextEditor view

change the background color of view when clicked on another view

TOP Ranking

HotTag

Archive