How can I make an image transparent on Android?
AndroidTransparencyAndroid Problem Overview
I am using a linear layout and frame layout. In the linear layout I keep an image as background and in the frame layout I keep an imageView. In that imageView I give an image.
Now I want to make the second image (that is in the imageView) transparent. How can I do this?
Android Solutions
Solution 1 - Android
Try this:
ImageView myImage = (ImageView) findViewById(R.id.myImage);
myImage.setAlpha(127); //value: [0-255]. Where 0 is fully transparent and 255 is fully opaque.
Note: setAlpha(int)
is deprecated in favor of setAlpha(float)
where 0 is fully transparent and 1 is fully opaque. Use it like: myImage.setAlpha(0.5f)
Solution 2 - Android
android:alpha
does this in XML:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/blah"
android:alpha=".75"/>
Solution 3 - Android
Set an id attribute on the ImageView:
<ImageView android:id="@+id/myImage"
In your code where you wish to hide the image, you'll need the following code.
First, you'll need a reference to the ImageView:
ImageView myImage = (ImageView) findViewById(R.id.myImage);
Then, set Visibility to GONE:
myImage.setVisibility(View.GONE);
If you want to have code elsewhere that makes it visible again, just set it to Visible the same way:
myImage.setVisibility(View.VISIBLE);
If you mean "fully transparent", the above code works. If you mean "partially transparent", use the following method:
int alphaAmount = 128; // Some value 0-255 where 0 is fully transparent and 255 is fully opaque
myImage.setAlpha(alphaAmount);
Solution 4 - Android
If you are in an XML file, use the following to make your imageview transparent!
android:background="@null"
Solution 5 - Android
On newer versions of Android (post Android 4.2 (Jelly Bean) at least), the setAlpha(int value) method is depreciated. Instead, use the setAlpha(float value)
method that takes a float between 0 and 1 where 0 is complete transparency and 1 is no transparency.
Solution 6 - Android
As setAlpha int has been deprecated, setImageAlpha (int) can be used
ImageView img = (ImageView) findViewById(R.id.img_image);
img.setImageAlpha(127); //value: [0-255]. Where 0 is fully transparent and 255 is fully opaque.
Solution 7 - Android
Set transparency using setAlpha(float alpha)
. The below code works for me were I used an alpha value in float, 0 - 1.
-
0: Full Transparent
-
0.5 - 50%: Transparent
-
1: Full Opaque
ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView); imageView.setImageResource(mResources[position]); imageView.setAlpha(.80f);
Solution 8 - Android
The method setAlpha(int)
from the type ImageView is deprecated.
Instead of
image.setImageAlpha(127);
//value: [0-255]. Where 0 is fully transparent and 255 is fully opaque.
Solution 9 - Android
In XML, use:
android:background="@android:color/transparent"
Solution 10 - Android
Image alpha sets just opacity to ImageView which makes Image blurry, try adding tint attribute in ImageView
android:tint="#66000000"
It can also be done programatically :
imageView.setColorFilter(R.color.transparent);
where you need to define transparent color in your colors.xml
<color name="transparent">#66000000</color>
Solution 11 - Android
For 20% transparency, this worked for me:
Button bu = (Button)findViewById(R.id.button1);
bu.getBackground().setAlpha(204);
Solution 12 - Android
Use:
ImageView image = (ImageView) findViewById(R.id.image);
image.setAlpha(150); // Value: [0-255]. Where 0 is fully transparent
// and 255 is fully opaque. Set the value according
// to your choice, and you can also use seekbar to
// maintain the transparency.