Android and setting alpha for (image) view alpha

235,908

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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).

Solution 5

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).

Share:
235,908
Ken
Author by

Ken

Updated on September 07, 2021

Comments

  • Ken
    Ken over 2 years

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

    If not, what alternatives are there?

  • Ken
    Ken about 13 years
    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.
  • Ken
    Ken about 13 years
    I want to set the alpha of an image using an XML attribute. Does this help?
  • Grant
    Grant about 13 years
    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?
  • Ken
    Ken about 13 years
    What do you mean "in the image itself"? Within the XML? An oversight it may be, but there's no alpha XML attribute.
  • Grant
    Grant about 13 years
    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.
  • Someone Somewhere
    Someone Somewhere almost 13 years
    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 :-(((((
  • Prakash Nadar
    Prakash Nadar almost 12 years
    Not really the best of doing it to solve the alpha of an image problem especially on the phone device.
  • Salw
    Salw almost 11 years
    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.
  • android developer
    android developer over 10 years
    i wonder : why is it deprecated? is it because now they have a float parameter?
  • noni
    noni over 10 years
    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 too
  • Toni Alvarez
    Toni Alvarez over 10 years
    Yes, you can use imageView.setAlpha(1.0f), but requires API level 11.
  • sschuberth
    sschuberth over 10 years
    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.
  • sschuberth
    sschuberth over 7 years
    @Antonio Why? This answer does not add any information on top of mine, and on the contrary is less complete.
  • Antonio
    Antonio over 7 years
    @sschuberth 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!
  • sschuberth
    sschuberth over 7 years
    @Antonio Done. I made my answer now super-duper-over-verbose ;-)
  • ataulm
    ataulm over 6 years
    the difference is that the acceptable range is 0-1 for the float one and 0-255 for the int one.