Skip to content

Instantly share code, notes, and snippets.

@mhemmings
Created January 6, 2014 21:48
Show Gist options
  • Save mhemmings/8290388 to your computer and use it in GitHub Desktop.
Save mhemmings/8290388 to your computer and use it in GitHub Desktop.
An android method to set the opacity of a view while supporting API 1+.
/**
* Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
* completely transparent and 1 means the view is completely opaque.
*
* @param alpha The opacity of the view.
* @param view The view to change.
*
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void setOpacity(View view, float alpha) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
view.setAlpha(alpha);
} else {
AlphaAnimation alphaAnim = new AlphaAnimation(alpha, alpha);
alphaAnim.setDuration(0);
alphaAnim.setFillAfter(true);
view.startAnimation(alphaAnim);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment