1

I have ImageViews which shows customer images. If the image of the customer is not available, I want to show gray background in ImageView.

Edit: It's a CircleImageview from 'de.hdodenhof:circleimageview:2.2.0' When I set the backgroundColor, It becomes a rectangle instead of circle.

Yesudass Moses
  • 1,841
  • 3
  • 27
  • 63
  • 1
    These links can help you: https://stackoverflow.com/questions/1492554/set-transparent-background-of-an-imageview-on-android and https://stackoverflow.com/questions/5445085/understanding-colors-on-android-six-characters/11019879#11019879 – Kush May 16 '18 at 14:29
  • I updated my answer for circular background – ColdFire May 16 '18 at 14:41

6 Answers6

2

use background attribute for your ImageView

Since the background needs to be circular use an shape xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <solid 
       android:color="#A9A9A9"/> <!-- you can set the color you want here -->

    <!-- you can also define size if required-->

</shape>

then use it as a background

android:background="@drawable/your_file_id"

This way when the image is showed it'll hide the background color.. if you don't set an image the background will be shown

Update
In order to avoid problems with partially transparent images and ratio remomve background when setting image resource

ColdFire
  • 6,764
  • 6
  • 35
  • 51
  • Unless there are transparent parts in the image and/or it's not covering the container completely... – HedeH May 16 '18 at 14:32
  • true.. they'll need to remove background when setting an image resource – ColdFire May 16 '18 at 14:36
  • There is no point in setting it in the xml if the Containing layout is being recycled and not inflated anew (Like in a RecyclerView), So I think it should be programatically anyway :) – HedeH May 16 '18 at 14:43
  • it'll depend on how the ImageView is implemented.. whether it changes the image before setting it or the view itself is circular.. based on the question it seems the view is normal since setting background make it become rectangular which means the view itself is not circular – ColdFire May 16 '18 at 14:45
2

If you are using a CircularImageView you can create a circular drawable:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <solid
        android:color="#808080"
        />

    <size
        android:height="120dp"
        android:width="120dp"
        />
</shape>

And then set it as a resource whenever the image of the customer is not available.

Suleyman
  • 2,765
  • 2
  • 18
  • 31
1

Use:

android:tint="@color/colorAccent"
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
1

If you need to do it dynamically that's the way:

Clear any previous image resource:

img.setImageResource(0);

and then

img.setBackgroundColor(Color.rgb(206, 206, 204));

or

img.setBackgroundResource(R.color.someGrayColor)

There are more options to set a background color to a View.. You can check online.

HedeH
  • 2,869
  • 16
  • 25
1
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">

<solid android:color="#808080"/>

<size android:height="25dp"android:width="25dp"/>
</shape>

When the image is not available, use this as a resource.

0

You can set the background transparent of any layout, any view, or any component by adding this code in XML:

android:background="@android:color/transparent" 
Kunal
  • 1
  • 3