8

This is just a minor question to speed things up a little bit, but I'm wondering if I can reference other colours and add additional information in my XML file.

I know that this code works:

<color name="primary1">#708FA3</color>
<color name="primary1_transparent">@color/primary1</color>

But I'm wondering is there a way to add a level of transparency to primary1_transparent? Either by concatenation in the assignment or afterwards. Something like this for example:

<color name="primary1">#708FA3</color>
<color name="primary1_transparent">#55 + @color/primary1</color>

I know that looks horrible and doesn't work, but hopefully it makes it clear what I'm looking to do.

Obviously the time saving on this isn't significant so an awkward workaround isn't going to help much but it seems like a possibly existing feature/hack I can't find.

SuperBiasedMan
  • 9,814
  • 10
  • 45
  • 73

3 Answers3

2

You can add transparency via XML itself by adding the following line to your view:

 android:alpha="0.25" 

You can adjust alpha value to increase or decrease the amount of transparency.

Example:

<View
    android:id="@+id/view1"
    android:layout_width="wrap_content"
    android:layout_height="150dp"
    android:alpha="0.25"
    android:background="@android:color/black"
    android:layout_alignParentLeft="true" />

This code made the Black background translucent: This

Tushar Gogna
  • 4,963
  • 6
  • 31
  • 42
  • Ah, this is a good idea for a workaround. The only caveats are that this affects the entire object (as opposed to possibly making background and text independently transparent) and has a minimum API level of 11. – SuperBiasedMan Dec 23 '14 at 12:59
  • This doesn't work if the object has a gradient background, and you only want to change the transparency of one end. – Flying_Banana Jul 23 '20 at 20:19
0

From seeing the Android documentation, within colors.xml you'll always have to declare something like this:

<color name=”your_color_name”>#AARRGGBB</color>

AA for alpha property.

I guess what you're trying to accomplish (applying alpha filters to defined colors) can only be done in the java part.

Tíbó
  • 1,188
  • 13
  • 28
-2

Yes of course you can add transparency level with color code.

Please try with below color code.

<color name="primary1">#1A708FA3</color>

I have added 1A before your color code which set 10% transparency level. Same way you can add all the code which i have listed below.

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
Deep Mehta
  • 1,237
  • 2
  • 11
  • 17
  • Sorry, I wasn't clear before. I know how to add transparency information, I just wanted to know if there was a way to add it to a reference of another colour in assignment. I edited my original question to make it clearer. – SuperBiasedMan Dec 23 '14 at 12:57