Set transparent background of an imageview on Android
Asked Answered
S

25

392

I am using a web view in which I am adding an image view. How can I set the background of this image view to transparent?

I have tried this:

mImageview.setBackgroundResource(R.color.trans);

Where trans<color name="trans">#00000000 </color>.

Saintjust answered 29/9, 2009 at 13:27 Comment(2)
This can help you : #5445585Fraase
I set #00000000 and it is transparent totally. Maybe there is some other error in your code?Knish
V
622

In your XML set the Background attribute to any colour, White(#FFFFFF) shade or Black(#000000) shade. If you want transparency, just put 80 before the actual hash code:

#80000000

This will change any colour you want to a transparent one.. :)

Vyky answered 29/7, 2011 at 12:50 Comment(11)
@Harshad: Is it possible to set, say, a 40% transparency for a background image set with View.setBackgroundResource(R.drawable.something)?Heloise
@LuisA.Florit I found this link where you need to multiply by 0.4(40/100) to your RGB value. Not sure if this is helpful but you can give it a try. forums.esri.com/Thread.asp?c=93&f=1730&t=223940Vyky
@Harshad: Sorry, I didn't understand your answer. I don't have any RGB value, but a TextView with a background image that I want to be, say, 50% transparent. But, anyway, I found the answer here in stackoverflow: View.getBackground().setAlpha(50). Couldn't be simpler, right? :o) Anyway, thanks for your help!!Heloise
If you add any number from 01 to 99 before the actual hash code, it will give you the transparency. Eg: Black with more transparency - #10000000 Black with less transparency - #99000000Relay
@AnhSirkDasarp Not only from 01 to 99, as the rest of the numbers these are also hexadecimal so you can put anything from 00 to ff. For fifty percent transparent you should put 80 instead of 50, and for the others you have some basic calculations to do...Uncinariasis
I had #4e4747 as the RGB value which used to give me solid brown color. Changing it to #804e4747 is giving me black color with little transparency. why ?Thigpen
try Color.TRANSPARENTLegofmutton
Thanks! I was trying to apply a gradient to an imageview so I had to set 80 to both the gradient and the background of the imageview.Cedilla
It is worth noting that this doesn't make it fully transparent. Use #00000000 instead if that's what you're going for.Viscoid
@MarcoSalerno May be it is working but it doesn't have anything behind it. Try to overlap the views to see if it is working.Vyky
this should help, what u should choose between "FF" and "00". tutorialspoint.com/…Mikesell
M
674

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" 
Martingale answered 10/3, 2012 at 12:31 Comment(8)
I had an issue on older versions of Android that didn't handle #ff000000 as pure transparent. Using this fixed it.Inpour
Probably because #FF 00 00 00 = 100% black.Stodgy
@Ioeschg The first 2 letters are for the alpha channel. #00xxxxxx would be transparent, whereas x is any hex value you want.Angiology
And the equivalent programatically if somebody is interested is Android.Resource.Color.Transparent :)Heterography
This shows as pure black on my app if i run it on the emulator using API 16 or 4.1.1. Any other workaround?Kibbutznik
@undroid try android:background="#00ffffff"Impetrate
try Color.TRANSPARENTLegofmutton
I get No resource found that matches the given name (at 'android:statusBarColor' with value '@color/transparent'). when I try thisDivot
V
622

In your XML set the Background attribute to any colour, White(#FFFFFF) shade or Black(#000000) shade. If you want transparency, just put 80 before the actual hash code:

#80000000

This will change any colour you want to a transparent one.. :)

Vyky answered 29/7, 2011 at 12:50 Comment(11)
@Harshad: Is it possible to set, say, a 40% transparency for a background image set with View.setBackgroundResource(R.drawable.something)?Heloise
@LuisA.Florit I found this link where you need to multiply by 0.4(40/100) to your RGB value. Not sure if this is helpful but you can give it a try. forums.esri.com/Thread.asp?c=93&f=1730&t=223940Vyky
@Harshad: Sorry, I didn't understand your answer. I don't have any RGB value, but a TextView with a background image that I want to be, say, 50% transparent. But, anyway, I found the answer here in stackoverflow: View.getBackground().setAlpha(50). Couldn't be simpler, right? :o) Anyway, thanks for your help!!Heloise
If you add any number from 01 to 99 before the actual hash code, it will give you the transparency. Eg: Black with more transparency - #10000000 Black with less transparency - #99000000Relay
@AnhSirkDasarp Not only from 01 to 99, as the rest of the numbers these are also hexadecimal so you can put anything from 00 to ff. For fifty percent transparent you should put 80 instead of 50, and for the others you have some basic calculations to do...Uncinariasis
I had #4e4747 as the RGB value which used to give me solid brown color. Changing it to #804e4747 is giving me black color with little transparency. why ?Thigpen
try Color.TRANSPARENTLegofmutton
Thanks! I was trying to apply a gradient to an imageview so I had to set 80 to both the gradient and the background of the imageview.Cedilla
It is worth noting that this doesn't make it fully transparent. Use #00000000 instead if that's what you're going for.Viscoid
@MarcoSalerno May be it is working but it doesn't have anything behind it. Try to overlap the views to see if it is working.Vyky
this should help, what u should choose between "FF" and "00". tutorialspoint.com/…Mikesell
M
189

In addition to what Harshad mentioned:

Two hexadecimal characters can be appended to any hexadecimal color code. The first two characters in an 8-digit hex color code represents its opacity in Android.

The two hexadecimal characters can range from 00 to FF. For example,

  • Normal opaque black hex- "#000000"
  • Fully transparent - "#00000000"
  • Fully opaque - "#FF000000"
  • 50% transparent - "#7F000000"

This way you can change any color to any level of transparency.

To find the hexadecimal prefix from a percentage:

Divide the percentage number by 100 and multiply by 255 to get the decimal value. Convert the decimal to hexadecimal here.

For example, for 50%, 50/100 * 255 = 127. Using the link we get hexadecimal value 7F.

Source: Android: how to create a transparent or opaque background

Marmalade answered 26/10, 2012 at 7:44 Comment(1)
actually, perfect 50% transparency is 7F and not 80 ;)Souther
D
185

If you want to add 20% or 30% transparency, you should pre-pend two more characters to the hexadecimal code, like CC.

Note

android:background="#CCFF0088" in XML

where CC is the alpha value, FF is the red factor, 00 is the green factor, and 88 is the blue factor.

Some opacity code:

Hex Opacity Values

100% — FF
95% — F2
90% — E6
85% — D9
80% — CC
75% — BF
70% — B3
65% — A6
60% — 99
55% — 8C
50% — 80
45% — 73
40% — 66
35% — 59
30% — 4D
25% — 40
20% — 33
15% — 26
10% — 1A
5%  — 0D
0% —  00

You can also set opacity programmatically like:

yourView.getBackground().setAlpha(127);

Set opacity between 0 (fully transparent) to 255 (completely opaque). The 127.5 is exactly 50%.

You can create any level of transparency using the given formula. If you want half transparent:

 16 |128          Where 128 is the half of 256.
    |8 -0         So it means 80 is half transparent.

And for 25% transparency:

16 |64            Where 64 is the quarter of 256.
   |4 -0          So it means 40 is quarter transparent.
Diggins answered 3/3, 2015 at 8:26 Comment(1)
This answer should be mark is the best. Thanks bro <3Kip
P
68

Use the below code for black:

<color name="black">#000000</color>

Now if you want to use opacity then you can use the below code:

<color name="black">#99000000</color>

And the below for opacity code:

100% — FF
95% — F2
90% — E6
85% — D9
80% — CC
75% — BF
70% — B3
65% — A6
60% — 99
55% — 8C
50% — 80
45% — 73
40% — 66
35% — 59
30% — 4D
25% — 40
20% — 33
15% — 26
10% — 1A
5% — 0D
0% — 00
Profile answered 19/5, 2016 at 12:51 Comment(0)
J
29

There is already a predefined constant. Use Color.TRANSPARENT.

Journal answered 20/3, 2013 at 9:1 Comment(1)
Use of a constant should be the best/cleanest way to go about it! But in this case, the value of Color.TRANSPARENT is set to 0x00000000 (not the expected fully opaque 0xFF000000), so depending on how you go about it, this constant might not really help accomplishing transparency! Still +1 for recommending a constant!Fogarty
F
25

There is already a transparent built into Android: R.color.transparent. http://developer.android.com/reference/android/R.color.html#transparent

But I think you may want to make the background of the image that you are placing into the WebView transparent, for example, with a transparent PNG, rather than the ImageView background. If the actual image is not at all see-through then the ImageView background can't be seen through it.

Failsafe answered 30/9, 2009 at 14:52 Comment(2)
the image is transparent..it didnt hav any background..i hv also tried setting imageview background as u hv said..but it didnt work...plz help..Saintjust
Can you post some more details such as a link to the image you are using and the code and layout you are using?Failsafe
L
23

Try this code :)

Its an fully transparent hexa code - "#00000000"

Luciferous answered 11/4, 2018 at 5:14 Comment(0)
A
20

In case you want it in code, just:

mComponentName.setBackgroundColor(Color.parseColor("#80000000"));
Advisee answered 31/5, 2012 at 13:7 Comment(0)
C
15

In your XML file, set an attribute "Alpha"

such as

android:alpha="0.0" // for transparent
android:alpha="1.0" // for opaque

You can give any value between 0.0 to 1.0 in decimal to apply the required transparency. For example, 0.5 transparency is ideal for disabled component

Cobby answered 11/6, 2017 at 3:43 Comment(0)
O
13

In xml

@android:color/transparent

In code

mComponentName.setBackgroundResource(android.R.color.transparent)
Olives answered 18/12, 2020 at 14:1 Comment(0)
S
12

Or, as an alternate, parse the resource ID with the following code:

  mComponentName.setBackgroundColor(getResources().getColor(android.R.color.transparent));
Savell answered 10/8, 2012 at 6:22 Comment(1)
is there an equivalent for androidX like androidx.R.color.transparent?Convince
I
6

In Android Studio it is very simple to adjust color and opacity using a built-in tool:

Android Adjust Color Opacity

Infamy answered 26/1, 2017 at 3:30 Comment(1)
just click on it. @PeterMortensenInfamy
H
5

For those who are still facing this problem, you may try this
element.getBackground().setAlpha(0);

Hester answered 1/8, 2013 at 2:47 Comment(2)
I get a NullPointerException: Attempt to invoke virtual method 'void android.graphics.drawable.Drawable.setAlpha(int)' on a null object referenceDawn
@IgorGanapolsky: try to watch your element see if it's a null objectHester
P
5

Use the following for complete transparency:

#00000000

When I tried with #80000000 I got a black transparent overlay which I don't want. Try to change the first two digits; it controls the level of transparency, like

#00000000
#10000000
#20000000
#30000000
Psych answered 11/12, 2014 at 4:16 Comment(1)
perfect color on transparent darkCulpa
E
5

One more simple way:

mComponentName.setBackgroundResource(android.R.color.transparent);
Emolument answered 27/2, 2015 at 9:51 Comment(0)
L
4

Another working option I came across is to set android:background="@null"

Lining answered 9/4, 2015 at 14:59 Comment(0)
B
2

You could also use View.setAlpha(float) to change the visibility precisely.

0 would be transparent, 1 fully visible. ;)

Barbette answered 18/3, 2013 at 21:29 Comment(2)
That changes the View's alpha, NOT its background!Dawn
The whole view will then be transparent including the content/picture in it, your setting the alpha which is wrong in this case. The user want's to keep the transparency of an Image not change the Image to be transparent.Rosalinarosalind
A
2

Try to use the following code. It will help you in full or more.

  1. A .xml file designed to use this code to set background color:

    android:background="#000000"
    

    or

    android:background="#FFFFFF"
    

    Image is here

    Or you can set it programmatically as well.

  2. Also you can use this code programmatically:

    image.setBackgroundDrawable(getResources().getDrawable(
        R.drawable.llabackground));
    
  3. Also this code for setting the background color as well programmatically:

    image.setBackgroundColor(Color.parseColor("#FFFFFF"));
    
  4. This code for the same programmatically:

    image.setBackgroundColor(getResources().getColor(Color.WHITE));
    

The color depends on your choice of which color you want to use for transparent. Mostly use a white or #FFFFFF color.

Regarding R.drawable.llabackground: This line of code is for your style of the background, like something special or different for your purpose. You can also use this.

Archimedes answered 27/5, 2015 at 12:53 Comment(0)
V
2

If you use a drawable XML image you can make it transparent as shown in the picture below, or you can use a color code:

<color name="black">#99000000</color> 

Enter image description here

Valencia answered 5/3, 2017 at 5:54 Comment(0)
F
1

Color definitions with transparency information may be in the form

#AARRGGBB or #ARGB.

You can use also the shorter value for full transparency: #0000.

Other values are e.g.:

white  grey   black
#FFFF  #F888  #F000 - full color
#EFFF  #E888  #E000
#DFFF  #D888  #D000
#CFFF  #C888  #C000
#BFFF  #B888  #B000
#AFFF  #A888  #A000
#9FFF  #9888  #9000
#8FFF  #8888  #8000
#7FFF  #7888  #7000
#6FFF  #6888  #6000
#5FFF  #5888  #5000
#4FFF  #4888  #4000
#3FFF  #3888  #3000
#2FFF  #2888  #2000
#1FFF  #1888  #1000
#0FFF  #0888  #0000 - full transparency
Freemason answered 31/1, 2020 at 21:0 Comment(0)
B
0

Try this:

#aa000000

For transparency 000000 = black, you can change these six numbers for the color you want.

Besnard answered 7/6, 2015 at 4:45 Comment(1)
what is your resource to use aa before color hexInfraction
R
0
ImageView.setBackground(R.drawable.my_background);

ImageView.setBackgroundResource(R.color.colorPrimary);

ImageView.getImageAlpha();

ImageView.setAlpha(125); // transparency
Rusk answered 3/11, 2016 at 21:57 Comment(1)
Although this code may help to solve the problem, it doesn't explain why and/or how it answers the question. Providing this additional context would significantly improve its long-term value. Please edit your answer to add explanation, including what limitations and assumptions apply.Lavoie
O
0

use RelativeLayout which has 2 imageViews in . and set transparency code on the top imageView.

transparency code :

<solid android:color="@color/white"/>
<gradient android:startColor="#40000000"   android:endColor="#FFFFFFFF" android:angle="270"/>
Occupy answered 4/1, 2021 at 8:27 Comment(0)
T
0

Convert Percentage to hex using any online tool & than simply add it on front of color value

Example: using https://www.joshuamiron.com/percent-to-hex-converter

enter image description here

80% opacity would be

 <color name="opaque_80_percent">#CC000000</color>
Tamatave answered 28/7, 2021 at 11:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.