in Android, Android Code Tips, Programming

Android and transparency

Alternative Title: How to make a view transparent or semi transparent.

When you want to create a transparent color in Android, you have to set 4 parameters.
Firstly, you have to set the Alpha component, which translates to: How much transparency on that color.
Secondly, you have to set the Red Green Blue (RGB) components.

(Read more for Alpha Compositing at Wikipedia)

Lets have an example:

//Will give you a solid red, Alpha component is set to max (255)
Color.argb(256, ff, 00, 00);

//Will give you just transparency, Alpha component is set to min (0)
Color.argb(0, ff, 00, 00);

//Will give you a red transparent color     
Color.argb(128, ff, 00, 00); 

You can use the above in several ways:
myView.setBackgroundColor(Color.argb(160, 75, 75, 75));
mPaint.setARGB(160, 75, 75, 75);

Do not forget that Android has also a predefined final value for transparency: Color.TRANSPARENT
that equals to Color.argb(0, 00, 00, 00);

Another way you can handle transparency is by playing with the xml view attributes like this:

<View 	android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:layout_gravity="top|center_horizontal" android:background="#00000000" />