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

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

Posted by: admin April 23, 2020 Leave a comment

Questions:

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.

How to&Answers:

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.

Answer:

Try using an AlphaAnimation : http://developer.android.com/reference/android/view/animation/AlphaAnimation.html


/* Have to use animation in order to get card faded. */

AlphaAnimation alpha = new AlphaAnimation(0.7F, 0.7F);
alpha.setDuration(0); // Make animation instant
alpha.setFillAfter(true); // Tell it to persist after the animation ends
view.startAnimation(alpha);

Answer:

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!

Answer:

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();
}

Answer:

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?