9

Can I programatically set the alpha of an ImageView without the need for being dependent on API level 11 where setAlpha was introduced?

1
  • 1
    Ohh.. there is a setAlpha(int alpha) as well in ImageView thats working from API level 1 :)
    – joynes
    Mar 13, 2012 at 21:16

4 Answers 4

40

ImageView has a method setAlpha(int) since API leve 1. So you can use it in any API level.
It is the setAlpha(float) method of View which is introduced in API level 11.

4
  • 1
    Use setImageAlpha(X) for SDK version >=16! Sep 28, 2014 at 5:55
  • I think the last comment is much more useful.
    – Vyacheslav
    Jun 13, 2015 at 18:52
  • setAlpha(int) is deprecated. Aug 13, 2015 at 13:58
  • It can't be the right answer, because it was asking for a way without setAlpha(), so f.e. with pure XML Aug 9, 2016 at 8:18
4

I used code to setAlpha of the image itself, not the view. This is available from API level 1..

public void toggleButton(int i) {
    if (indImageBtnEnabled[i]) {

        int di = getDrawableId(findViewById(myImagebtns[i]));
        Drawable d = this.getResources().getDrawable(di);
        d.setAlpha(25);

        ((ImageView) findViewById(myImagebtns[i])).setImageDrawable(d);

        indImageBtnEnabled[i] = false;
    } else {
        // findViewById(myImagebtns[i]).setAlpha(1f) << NEEDS API11;
        int di = getDrawableId(findViewById(myImagebtns[i]));
        Drawable d = this.getResources().getDrawable(di);
        d.setAlpha(255);

        ((ImageView) findViewById(myImagebtns[i])).setImageDrawable(d);

        indImageBtnEnabled[i] = true;
    }
}
2

I think (But don't know for sure) that you could apply an alpha animation to your ImageView, and as long as the fillAfter() is set to true on the animation the Image should stay at whatever alpha level it ends at.

2
  • I just found out the simpler answer :) ... see above.
    – joynes
    Mar 13, 2012 at 21:17
  • that is definitely the way to go then
    – FoamyGuy
    Mar 13, 2012 at 21:27
2

you can make a new layout with just ur image, set the alpha you need for the layout, set it as an overlay and inflate when you want.

LayoutInflater inflater = LayoutInflater.from(this);
        View overView = inflater.inflate(R.layout.myAlpahImageLayout, null);
        this.addContentView(overView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:alpha="0.5"
    tools:context=".MainActivity" >

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

</RelativeLayout>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.