0

I have a Question, I play a lot with opacity while defining colors, but there is a problem in android, U have to define all colors in colors.xml file & I have already defined thier about 145 colors in colors.xml file,

Now the problem is if i write in colors.xml for all types of opacity values for each color, As you know there are 21 opacity types e.g:

Example:

<color name="black">#000000</color>
<color name="a_black">#FF000000</color>
<color name="b_black">#F2000000</color>
<color name="c_black">#D9000000</color>
<color name="d_black">#CC000000</color>......

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

This defining all colors with opacities means 21 * 145 = 3045 colors and this will take hours to write in colors.xml file and will make the file large.

  1. Is there any way to have all colors with opacities in 'colors.xml' file without wasting time and without making colors.xml file large?.
  2. Is there possible that I can write those 145 colors on 'colors.xml' file but change their opacity when using it for any background property or for any text color property in activity_ main.xml?, If it is possible then how can we do that?

Thanks In Advance.

Reaz Murshed
  • 21,071
  • 12
  • 69
  • 87
kamiCoder
  • 3
  • 2
  • You don't have to specify all colors in colors.xml. Also, no idea where you're getting this 21 opacities thing. There's a full byte for opacity, so you can have up to 256. ANy "21 opacities" idea is something some system you're trying to used make up. Just define the colors you actually use, and skip the rest. I can't even find any google references with "21 opacities" other than this question. – Gabe Sechan Jun 02 '18 at 11:41

1 Answers1

0

You can do that programmatically. You need to define the base colors in your colors.xml and then control the opacity from your code wherever necessary.

colorWith25Opacity = (R.color.black & 0x00FFFFFF) | 0x40000000;

You might also consider using this library to get the color code of your desired color like this.

String colorCode = ColorTransparentUtils.transparentColor10(R.color.a_black);

Check the answers provided here and here.

Reaz Murshed
  • 21,071
  • 12
  • 69
  • 87
  • Thanks for Your reply It works & thanks for the help but is it possible that we can change opacity during the time of declaration while defining property in activity_main.xml file Example: Such as like attaching FF before the reference of color? – kamiCoder Jun 02 '18 at 12:58
  • Great to know that helped. However, I think it is not possible the way you want by defining the color directly from layout. – Reaz Murshed Jun 02 '18 at 19:12