How to make a background 20% transparent on Android
You can manage color opacity changing the first 2 characters in the color definition:
#99000000
100% — FF
99% — FC
98% — FA
97% — F7
96% — F5
95% — F2
94% — F0
93% — ED
92% — EB
91% — E8
90% — E6
89% — E3
88% — E0
87% — DE
86% — DB
85% — D9
84% — D6
83% — D4
82% — D1
81% — CF
80% — CC
79% — C9
78% — C7
77% — C4
76% — C2
75% — BF
74% — BD
73% — BA
72% — B8
71% — B5
70% — B3
69% — B0
68% — AD
67% — AB
66% — A8
65% — A6
64% — A3
63% — A1
62% — 9E
61% — 9C
60% — 99
59% — 96
58% — 94
57% — 91
56% — 8F
55% — 8C
54% — 8A
53% — 87
52% — 85
51% — 82
50% — 80
49% — 7D
48% — 7A
47% — 78
46% — 75
45% — 73
44% — 70
43% — 6E
42% — 6B
41% — 69
40% — 66
39% — 63
38% — 61
37% — 5E
36% — 5C
35% — 59
34% — 57
33% — 54
32% — 52
31% — 4F
30% — 4D
29% — 4A
28% — 47
27% — 45
26% — 42
25% — 40
24% — 3D
23% — 3B
22% — 38
21% — 36
20% — 33
19% — 30
18% — 2E
17% — 2B
16% — 29
15% — 26
14% — 24
13% — 21
12% — 1F
11% — 1C
10% — 1A
9% — 17
8% — 14
7% — 12
6% — 0F
5% — 0D
4% — 0A
3% — 08
2% — 05
1% — 03
0% — 00
Make the color have 80% in the alpha channel. For example, for red use #CCFF0000
:
<TextView
...
android:background="#CCFF0000" />
In the example, CC
is the hexadecimal number for 255 * 0.8 = 204
. Note that the first two hexadecimal digits are for the alpha channel. The format is #AARRGGBB
, where AA
is the alpha channel, RR
is the red channel, GG
is the green channel and BB
is the blue channel.
I'm assuming that 20% transparent means 80% opaque. If you meant the other way, instead of CC
use 33
which is the hexadecimal for 255 * 0.2 = 51
.
In order to calculate the proper value for an alpha transparency value you can follow this procedure:
- Given a transparency percentage, for example 20%, you know the opaque percentage value is 80% (this is
100-20=80
) - The range for the alpha channel is 8 bits (
2^8=256
), meaning the range goes from 0 to 255. - Project the opaque percentage into the alpha range, that is, multiply the range (255) by the percentage. In this example
255 * 0.8 = 204
. Round to the nearest integer if needed. - Convert the value obtained in 3., which is in base 10, to hexadecimal (base 16). You can use Google for this or any calculator. Using Google, type "204 to hexa" and it will give you the hexadecimal value. In this case it is
0xCC
. - Prepend the value obtained in 4. to the desired color. For example, for red, which is
FF0000
, you will haveCCFF0000
.
You can take a look at the Android documentation for colors.
Use the below code for black:
<color name="black">#000000</color>
Now if I want to use opacity then you can use the below code:
<color name="black">#99000000</color> <!-- 99 is for alpha and others pairs zero's are for R G B -->
And below for opacity code: and all opacity level here
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
If you always to forget what code for transparency then you must have to see below link and no worry about to remember anything regarding transparent code :-
https://github.com/duggu-hcd/TransparentColorCode
textviewHeader.setTextColor(Color.parseColor(ColorTransparentUtils.transparentColor(R.color.border_color,10)));