Android ImageView Transparent Background

The source bitmap for ImageView has transparent color, we must set the background color of this ImageView to transparent color.

If not, an ImageView will overlap with another ImageView.

Those are some methods to set transparent color.

Static Setting

In xml, set background “#00000000”

activity.xml
1
2
3
4
5
<ImageView
android:layout_width="218dp"
android:layout_height="247dp"
android:src="@drawable/test2"
android:background="#00000000"/>

Also can use android color : transparent

activity.xml
1
2
3
4
5
<ImageView
android:layout_width="218dp"
android:layout_height="247dp"
android:src="@drawable/test2"
aandroid:background="@android:color/transparent"/>

Dynamic Setting

In program, we could use API: setBackgroundColor.

MainActivity.java
1
2
var imageView = findViewById<ImageView>(R.id.test1)
imageView.setBackgroundColor(Color.parseColor("#00000000"))

Or use Color.TRANSPARENT

MainActivity.java
1
2
var imageView = findViewById<ImageView>(R.id.test1)
imageView.setBackgroundColor(Color.TRANSPARENT)

Result

Author

Nick Lin

Posted on

2019-12-26

Updated on

2023-01-18

Licensed under


Comments