west44
west44

Reputation: 749

How to change background color of ImageView which use setBackgroundResource() from java code?

I am using the <shape> tag like this:

<?xml version="1.0" encoding="UTF-8"?>
  <shape xmlns:android="http://schemas.android.com/apk/res/android">
  <solid android:color="#00FFFFFF" />
  <stroke android:width="1dp" android:color="#FFFFFFFF" />
  <padding android:left="1dp" android:top="1dp" android:right="1dp"
    android:bottom="1dp" />
</shape>

I have a lot of views I created with the <shape> tag in a GridView and I need to change the part that was set here:

<solid android:color="#00FFFFFF" />

and set another color, but I don't know how... I tried to use ImageView.Adapter like this:

imageView = new ImageView(context);
        imageView.setLayoutParams(new GridView.LayoutParams(160, 105));
        imageView.setBackgroundColor(myBackgroundColors[position]);
        imageView.setBackgroundResource(R.drawable.border);

I set the background color before I call setBackgroundResource() and in XML I use transparent color, but it doesn't work...any ideas?

Upvotes: 1

Views: 6689

Answers (1)

matt5784
matt5784

Reputation: 3085

When using the <shape /> (Shape Drawable) tag in xml the compiled resource is not an imageview; it compiles into a GradientDrawable. The <solid /> tag defines a solid color to fill the defined shape and sets it as android:color. Thus, to change this programatically you should use the setColor() method.

Upvotes: 3

Related Questions