Android and setting alpha for (image) view alpha
Asked Answered
P

10

151

Is there really no XML attribute counterpart to setAlpha(int)?

If not, what alternatives are there?

Premiere answered 8/2, 2011 at 8:46 Comment(0)
C
221

No, there is not, see how the "Related XML Attributes" section is missing in the ImageView.setAlpha(int) documentation. The alternative is to use View.setAlpha(float) whose XML counterpart is android:alpha. It takes a range of 0.0 to 1.0 instead of 0 to 255. Use it e.g. like

<ImageView android:alpha="0.4">

However, the latter in available only since API level 11.

Currant answered 23/1, 2013 at 12:4 Comment(2)
Even if I'm just repeating myself here: ImageView.setAlpha(int) is taking an int while android:alpha is taking a float, so strictly speaking the latter is not the exact XML counterpart to the former, but it's the counterpart to View.setAlpha(float). And as mentioned multiple times here already, android:alpha / View.setAlpha(float) are available as of API level 11 only.Currant
the difference is that the acceptable range is 0-1 for the float one and 0-255 for the int one.Saari
R
258

It's easier than the other response. There is an xml value alpha that takes double values.

android:alpha="0.0" thats invisible

android:alpha="0.5" see-through

android:alpha="1.0" full visible

That's how it works.

Roam answered 7/5, 2013 at 10:55 Comment(3)
There is setAlpha(float) and android:alpha only since API 11 (Android 3.0). Prior API 11 one must use code to set alpha for image. As sschuberth already said in anser above.Hydroxy
@Antonio Why? This answer does not add any information on top of mine, and on the contrary is less complete.Currant
@Currant your answer is completely correct, but the lack of an example could make this answer take more attention than yours. Although your answer provides more information, this answer provides a solution closer to what I really need. Please, add some examples to use what you have explained, that absolutely will help!Sylviesylvite
C
221

No, there is not, see how the "Related XML Attributes" section is missing in the ImageView.setAlpha(int) documentation. The alternative is to use View.setAlpha(float) whose XML counterpart is android:alpha. It takes a range of 0.0 to 1.0 instead of 0 to 255. Use it e.g. like

<ImageView android:alpha="0.4">

However, the latter in available only since API level 11.

Currant answered 23/1, 2013 at 12:4 Comment(2)
Even if I'm just repeating myself here: ImageView.setAlpha(int) is taking an int while android:alpha is taking a float, so strictly speaking the latter is not the exact XML counterpart to the former, but it's the counterpart to View.setAlpha(float). And as mentioned multiple times here already, android:alpha / View.setAlpha(float) are available as of API level 11 only.Currant
the difference is that the acceptable range is 0-1 for the float one and 0-255 for the int one.Saari
D
56

I am not sure about the XML but you can do it by code in the following way.

ImageView myImageView = new ImageView(this);
myImageView.setAlpha(xxx);

In pre-API 11:

  • range is from 0 to 255 (inclusive), 0 being transparent and 255 being opaque.

In API 11+:

  • range is from 0f to 1f (inclusive), 0f being transparent and 1f being opaque.
Deidredeific answered 8/2, 2011 at 9:25 Comment(3)
Yep, I know. (I hoped this was implicit in the question.) One point of XML is to strip out some of this code. It doesn't make sense to me why alpha does not have an XML-attribute counterpart when various sizes, positions do.Premiere
i wonder : why is it deprecated? is it because now they have a float parameter?Noella
Yes, you can use imageView.setAlpha(1.0f), but requires API level 11.Quaternity
N
12

Maybe a helpful alternative for a plain-colored background:

Put a LinearLayout over the ImageView and use the LinearLayout as a opacity filter. In the following a small example with a black background:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF000000" >

<RelativeLayout
    android:id="@+id/relativeLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/icon_stop_big" />

    <LinearLayout
        android:id="@+id/opacityFilter"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#CC000000"
        android:orientation="vertical" >
    </LinearLayout>
</RelativeLayout>

Vary the android:background attribute of the LinearLayout between #00000000 (fully transparent) and #FF000000 (fully opaque).

Naaman answered 6/2, 2012 at 19:24 Comment(2)
Not really the best of doing it to solve the alpha of an image problem especially on the phone device.Palanquin
This works great when you just want to make background transparent but NOT its childs. Using alpha on the parent container, makes all its childs transparent tooHueston
J
9

There is now an XML alternative:

        <ImageView
        android:id="@+id/example"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/example"
        android:alpha="0.7" />

It is: android:alpha="0.7"

With a value from 0 (transparent) to 1 (opaque).

Juju answered 5/8, 2013 at 18:59 Comment(0)
D
6

setAlpha(int) is deprecated as of API 16: Android 4.1

Please use setImageAlpha(int) instead

Dooley answered 23/8, 2018 at 8:25 Comment(0)
O
4

use android:alpha=0.5 to achieve the opacity of 50% and to turn Android Material icons from Black to Grey.

Overabound answered 16/9, 2016 at 14:47 Comment(0)
T
3

Use this form to ancient version of android.

ImageView myImageView;
myImageView = (ImageView) findViewById(R.id.img);

AlphaAnimation alpha = new AlphaAnimation(0.5F, 0.5F);
alpha.setDuration(0); 
alpha.setFillAfter(true); 
myImageView.startAnimation(alpha);
Tribunal answered 30/7, 2013 at 19:28 Comment(0)
V
2

To reduce the opacity of anything in XML Android, use the Attribute Alpha. Example:

android:alpha="0.6"

You must enter the value between range of 0.0 to 1.0, in points.

Vasculum answered 6/9, 2021 at 5:51 Comment(0)
F
0

The alpha can be set along with the color using the following hex format #ARGB or #AARRGGBB. See http://developer.android.com/guide/topics/resources/color-list-resource.html

Flaring answered 19/2, 2011 at 0:47 Comment(5)
I want to set the alpha of an image using an XML attribute. Does this help?Premiere
I am using to this to bleand layouts over a layout with a background image, surely you would set the the alpha of an image in the image itself?Flaring
What do you mean "in the image itself"? Within the XML? An oversight it may be, but there's no alpha XML attribute.Premiere
I mean when you create the image you would add a transparency layer and set the opacity of your image to 50 or whatever value you require.Flaring
I am VERY surprised that android:tint has no effect. It allows for an alpha value... but it has not effect on a white image :-(((((Lodovico

© 2022 - 2024 — McMap. All rights reserved.