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));
or
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" />
Share

Android ListView black background when scrolling problem

ListViews in Android as explained here uses Android’s black color as the default background. If you change this background the default behavior is for the ListView to cache the default color for optimazation.

To fix this issue, all you have to do is either disable the cache color hint optimization, if you use a non-solid color background, or set the hint to the appropriate solid color value. This can be dome from code or preferably from XML, by using the android:cacheColorHint attribute. To disable the optimization, simply use the transparent color #00000000.

example:

<ListView android:id="@android:id/list" android:layout_width="fill_parent"
		android:layout_height="fill_parent" android:layout_weight="1"
		android:layout_gravity="top" android:cacheColorHint="#00000000">
Share