How to Set Opacity (Alpha) for View in Android

How to Set Opacity (Alpha) for View in Android

I just found your question while having the similar problem with a TextView. I was able to solve it, by extending TextView and overriding onSetAlpha. Maybe you could try something similar with your button:

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

public class AlphaTextView extends TextView {

public AlphaTextView(Context context) {
super(context);
}

public AlphaTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public AlphaTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
public boolean onSetAlpha(int alpha) {
setTextColor(getTextColors().withAlpha(alpha));
setHintTextColor(getHintTextColors().withAlpha(alpha));
setLinkTextColor(getLinkTextColors().withAlpha(alpha));
return true;
}
}

Android and setting alpha for (image) view alpha

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.

Change the opacity/transparency of a view and affect the child views too

That's because you are changing the backgound of the layout and not the layout itself. So instead of myRelativeLayout.getBackground().setAlpha(), use this: myRelativeLayout.setAlpha(0.4f).

How to make a background 20% transparent on Android

Make the color have 80% in the alpha channel. For example, for red use #CCFF0000:

<TextView
...
android:background="#CCFF0000" />

In the example, CC is the hexadecimal number for 255 * 0.8 = 204. Note that the first two hexadecimal digits are for the alpha channel. The format is #AARRGGBB, where AA is the alpha channel, RR is the red channel, GG is the green channel and BB is the blue channel.

I'm assuming that 20% transparent means 80% opaque. If you meant the other way, instead of CC use 33 which is the hexadecimal for 255 * 0.2 = 51.

In order to calculate the proper value for an alpha transparency value you can follow this procedure:

  1. Given a transparency percentage, for example 20%, you know the opaque percentage value is 80% (this is 100-20=80)
  2. The range for the alpha channel is 8 bits (2^8=256), meaning the range goes from 0 to 255.
  3. Project the opaque percentage into the alpha range, that is, multiply the range (255) by the percentage. In this example 255 * 0.8 = 204. Round to the nearest integer if needed.
  4. Convert the value obtained in 3., which is in base 10, to hexadecimal (base 16). You can use Google for this or any calculator. Using Google, type "204 to hexa" and it will give you the hexadecimal value. In this case it is 0xCC.
  5. Prepend the value obtained in 4. to the desired color. For example, for red, which is FF0000, you will have CCFF0000.

You can take a look at the Android documentation for colors.

Set opacity for TextView


your_variable_name.setALpha(0f);

f = float

0f means 0%

1f means 100%

0.8f means 80%

You can use setAlpha() in ImageViews also.

If you want to smooth the process(use an animation) :

your_variable_name.animate().alpha(0f).setDuration(300);

300 is millis that means 1000 = 1 sec

how to change the alpha value of imageview

Maybe you can try like this, I had troubles doing it normal way but with "animation" it was ok.

AlphaAnimation alpha = new AlphaAnimation(0.5F, 0.5F); // change values as you want
alpha.setDuration(0); // Make animation instant
alpha.setFillAfter(true); // Tell it to persist after the animation ends
// And then on your imageview
yourImageView.startAnimation(alpha);

Set Alpha/Opacity of Layout

You can set the alpha on the layout and it's children (or any other view for that matter) using AlphaAnimation with 0 duration and setFillAfter option.

Example:

AlphaAnimation alpha = new AlphaAnimation(0.5F, 0.5F);
alpha.setDuration(0); // Make animation instant
alpha.setFillAfter(true); // Tell it to persist after the animation ends
// And then on your layout
yourLayout.startAnimation(alpha);

You can use one animation for multiple components to save memory. And do reset() to use again, or clearAnimation() to drop alpha.

Though it looks crude and hacked it's actually a good way to set alpha on set ov views that doesn't take much memory or processor time.

Not sure about getting current alpha value though.



Related Topics



Leave a reply



Submit