151

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

If not, what alternatives are there?

Rahul Tiwari
  • 6,851
  • 3
  • 49
  • 78
Ken
  • 30,811
  • 34
  • 116
  • 155

10 Answers10

254

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.

Gautam Krishnan
  • 722
  • 2
  • 10
  • 28
jfcogato
  • 3,359
  • 3
  • 19
  • 19
  • 16
    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. – Salw Jun 17 '13 at 11:21
  • @Antonio Why? This answer does not add any information on top of mine, and on the contrary is less complete. – sschuberth Nov 20 '16 at 12:27
  • @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! – Antonio Nov 21 '16 at 14:30
  • @Antonio Done. I made my answer now super-duper-over-verbose ;-) – sschuberth Nov 21 '16 at 14:42
219

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.

sschuberth
  • 28,386
  • 6
  • 101
  • 146
  • 7
    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 Nov 19 '13 at 20:33
  • the difference is that the acceptable range is 0-1 for the float one and 0-255 for the int one. – ataulm Nov 09 '17 at 10:37
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.
Sufian
  • 6,405
  • 16
  • 66
  • 120
Umesh
  • 4,406
  • 2
  • 25
  • 37
  • 2
    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 Feb 08 '11 at 09:31
  • i wonder : why is it deprecated? is it because now they have a float parameter? – android developer Oct 03 '13 at 10:58
  • Yes, you can use imageView.setAlpha(1.0f), but requires API level 11. – Toni Alvarez Oct 30 '13 at 23:36
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).

almic
  • 17
  • 6
marnaish
  • 2,266
  • 1
  • 21
  • 17
  • 1
    Not really the best of doing it to solve the alpha of an image problem especially on the phone device. – Prakash Nadar May 11 '12 at 14:41
  • 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 – noni Oct 09 '13 at 19:34
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).

Sorcerer
  • 854
  • 1
  • 8
  • 20
6

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

Please use setImageAlpha(int) instead

Alen Lee
  • 2,479
  • 2
  • 21
  • 30
4

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

Sachiin Gupta
  • 378
  • 2
  • 9
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);
Rafael
  • 31
  • 3
1

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.

Rehan Khan
  • 1,031
  • 13
  • 10
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

Grant
  • 258
  • 2
  • 8
  • I want to set the alpha of an image using an XML attribute. Does this help? – Ken Feb 19 '11 at 06:49
  • 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? – Grant Mar 09 '11 at 12:17
  • What do you mean "in the image itself"? Within the XML? An oversight it may be, but there's no alpha XML attribute. – Ken Mar 09 '11 at 12:44
  • 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. – Grant Mar 17 '11 at 15:19
  • 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 :-((((( – Someone Somewhere Jun 24 '11 at 20:29