33

How can I change the transparency (alpha) of a view on pre-SDK-11 on Android?

Before you suggest using a background colour with some transparency, please note that this method does not include all elements in the view such as the text of a button or the child views of a view group.

Gallal
  • 4,267
  • 6
  • 38
  • 61

4 Answers4

26

The ViewHelper of NineOldAndroids is what I use, it is a static helper class and a real gem! Many here recommend NineOldAndroids but I have seen no mention of the ViewHelper. It is really easy to use.

import com.nineoldandroids.view.ViewHelper;
...

ViewHelper.setAlpha(myView, .2f);

You can also use it to set other properties like x, y etc, very handy when setting up for animations or building your UI. Many thanks to Jake Wharton for sharing his work with the community!

lejonl
  • 1,453
  • 15
  • 20
15

EDIT - example below refers for Android pre-SDK11, but I just found out about an amazingly great library called Nine Old Androids, The amazing thing it does is enabling all animation capabilities of Android 3.0 for all API versions!!!

Previous answer

I actually encountered this kind of problem when wanted to set alpha dynamically on a complex layout. I created an override of onSetAlpha() and added another recursive function that checks every kind of view for background image, drawables and text colors.

    @Override
public boolean onSetAlpha(int alpha)
{
    return onSetAlpha(alpha, theLayoutYouWantToSetAlphaTo);
}

public boolean onSetAlpha(int alpha, View view)
{
    if (view instanceof ViewGroup)
    {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++)
        {
            onSetAlpha(alpha, ((ViewGroup) view).getChildAt(i));
            if (((ViewGroup) view).getBackground() != null) ((ViewGroup) view).getBackground().setAlpha(alpha);
        }
    }
    else if (view instanceof ImageView)
    {
        if (((ImageView) view).getDrawable() != null) ((ImageView) view).getDrawable().setAlpha(alpha);
        if (((ImageView) view).getBackground() != null) ((ImageView) view).getBackground().setAlpha(alpha);
    }
    else if (view instanceof TextView)
    {
        ((TextView) view).setTextColor(((TextView) view).getTextColors().withAlpha(alpha));
        if (((TextView) view).getBackground() != null) ((TextView) view).getBackground().setAlpha(alpha);
    }
    else if (view instanceof EditText)
    {
        ((EditText) view).setTextColor(((EditText) view).getTextColors().withAlpha(alpha));
        if (((EditText) view).getBackground() != null) ((EditText) view).getBackground().setAlpha(alpha);
    }
    return true;
}

You can add other kinds of views as you need.

Rotemmiz
  • 7,933
  • 3
  • 36
  • 36
  • 3
    Sadly you can't use the nineoldandroids method. If you do something like `ObjectAnimator.ofFloat(myview, "alpha", 0.1f, 0.0f).setDuration(10).start();` it just says "couldn't find setter property alpha for myview with value type float.". – Timmmm Nov 01 '12 at 15:59
  • 1
    Have you tried looking at this? http://jakewharton.com/advanced-pre-honeycomb-animation/ – Martin Rajniak Feb 12 '13 at 10:36
  • your parameter of `int alpha` is closed at [0, 255], but not [0.0f, 1.0f], right? – SilentKnight Jun 15 '15 at 10:12
9

You can extend the views draw() method and use the canvas.saveAlphaLayer()

public void draw(Canvas canvas) {
    canvas.saveLayerAlpha(null, alphaValue, ALL_SAVE_FLAG);
    super.draw(canvas);
    canvas.restore();
}
Community
  • 1
  • 1
Navin Ilavarasan
  • 1,271
  • 11
  • 15
0

You can set Alpha to all colors of the view.(such as the text of a button or the child views of a view group). Make them into colors xml and use in all View.

You can read colors from the view recursively and add alpha to them and set them back.

You can create the view as a main view of a new Activity. Then do as in How do I create a transparent Activity on Android?

Community
  • 1
  • 1
Gangnus
  • 24,044
  • 16
  • 90
  • 149