Reputation: 1879
I want set a background colour to an ImageView, when the imageview has an image resource, it works and i get my background colour but when the image view don't have an image resource, SetBackgroundColor don't work and do nothing, this is my code :
xml Layout :
<ImageView android:layout_centerHorizontal="true"
android:id="@+id/favor_item_image"
android:layout_alignParentTop="true" android:layout_height="wrap_content"
android:layout_width="match_parent" android:adjustViewBounds="true"/>
java code :
ImageView favorImage = (ImageView) MyView.findViewById(R.id.favor_item_image);
//favorImage.setImageResource(R.drawable.btndelete);
favorImage.setBackgroundColor(Color.argb(255,255,0,0));
So, when i comment this line : //favorImage.setImageResource(R.drawable.btndelete); i cannot set the image view background colour , but when i uncomment this line, setBackgroundColor works fine.
any help please. Thanks
Upvotes: 4
Views: 8912
Reputation: 37516
Your ImageView
has a height of wrap_content
, and when you only set the background color, it has no content. Therefore doesn't show up. You should be able to fix this by setting a different height.
Upvotes: 1
Reputation: 24857
The reason you are not seeing anything is because you have the layout_height set to wrap_content. When you do not have an image set on the ImageView wrap_content sets the height to 0. If you want the color to display without an image you will have to set the layout_height to an actual value such as 50dp.
Upvotes: 6
Reputation: 20319
Try setting: android:adjustViewBounds=false
I may be mistaken but when this is set to true
the view might scale to 0 width and 0 height when it doesn't have an image.
Upvotes: 0