How set alpha/opacity value to color on xml drawable? How set alpha/opacity value to color on xml drawable? android android

How set alpha/opacity value to color on xml drawable?


i may be a little late, but if someone else comes to this post and is looking for some alpha values. Jared Rummler did the work and provids us with every posibile valuehttps://stackoverflow.com/a/27813407/5973229

So he uses this Method to calculate every opacity value in Hex code:

for (double i = 1; i >= 0; i -= 0.01) {i = Math.round(i * 100) / 100.0d;int alpha = (int) Math.round(i * 255);String hex = Integer.toHexString(alpha).toUpperCase();if (hex.length() == 1) hex = "0" + hex;int percent = (int) (i * 100);System.out.println(String.format("%d%% — %s", percent, hex));

And then this is the Result:

100% — FF99% — FC98% — FA97% — F796% — F595% — F294% — F093% — ED92% — EB91% — E890% — E689% — E388% — E087% — DE86% — DB85% — D984% — D683% — D482% — D181% — CF80% — CC79% — C978% — C777% — C476% — C275% — BF74% — BD73% — BA72% — B871% — B570% — B369% — B068% — AD67% — AB66% — A865% — A664% — A363% — A162% — 9E61% — 9C60% — 9959% — 9658% — 9457% — 9156% — 8F55% — 8C54% — 8A53% — 8752% — 8551% — 8250% — 8049% — 7D48% — 7A47% — 7846% — 7545% — 7344% — 7043% — 6E42% — 6B41% — 6940% — 6639% — 6338% — 6137% — 5E36% — 5C35% — 5934% — 5733% — 5432% — 5231% — 4F30% — 4D29% — 4A28% — 4727% — 4526% — 4225% — 4024% — 3D23% — 3B22% — 3821% — 3620% — 3319% — 3018% — 2E17% — 2B16% — 2915% — 2614% — 2413% — 2112% — 1F11% — 1C10% — 1A9% — 178% — 147% — 126% — 0F5% — 0D4% — 0A3% — 082% — 051% — 030% — 00


The color "appears" totally transparent b/c it is almost totally transparent. Hex colors are normally 6 digits #RRGGBB but if you would like to set the opacity you pass in 2 digits in hexadecimal scale (base-16) at the start, so in your case #20C0C0C0 your opacity is 20 (in base-16).

Here are some typical decimal alpha values mapped to hexadecimal ones

  • 0 -> 0 (this is completely transparent)
  • 32 -> 20 (this is your opacity)
  • 255 -> FF (this is completely opaque)

So your opacity is only ~12%.

If you would like it to be more opaque (less transparent) use a higher number (7F will give you about 50% opacity)

So:

<solid android:color="#7FC0C0C0" />


Giving an updated answer for when people see this, post 2018. As much as I've seen, when developing for API versions > Lollipop, one does not need to use hexadecimal values for alpha. I checked this yesterday and was surprised by this as well.

In the android color schemes, we use Hex values as in #FFFFFF for the color white. This represents RGB channels in said color. In otherwords, as mentioned by @Cumulo Nimbus above, #RRGGBB.

For opacity/transparency, you need to include the alpha channel as well which would give #AARRGGBB. In decimal/denary (base 10), R,G,B Channels each range from 0-255 whilst the alpha channel ranges from 0-100 so if I wanted to use the color sample you provided with 50% opacity, All I need to do is something like:

<solid android:color="#50C0C0C0"/>

where 50 is the percentage of opacity you require. For a fully opaque option i.e. 100% opacity, you do not need to use #FFC0C0C0 or #100C0C0C0(Note: this would show nothing). Just leave it as #C0C0C0 instead.