How to setAlpha for View in Android 2.3 (API-10) and lower

Published by Igor Khrupin on

As you can see there existing setAlpha() method in API-11. Using this method you can change Alpha for View.

What we gonna do in API-10 and lower to change View Alpha?

I’ve got one solution. The solution is AlphaAnimation

Please take a look my code:

package com.hrupin.samples.alpha;

import com.hrupin.samples.halfopacitywidgets.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.Button;

public class MainActivity extends Activity {

	private Button buttonWithAlpha;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		buttonWithAlpha = (Button)findViewById(R.id.buttonWithOpacity);

		setAlphaForView(buttonWithAlpha, 0.5f);
	}

	private void setAlphaForView(View v, float alpha) {
		AlphaAnimation animation = new AlphaAnimation(alpha, alpha);
		animation.setDuration(0); 
		animation.setFillAfter(true); 
		v.startAnimation(animation);
	}
}

In this way you can simply change alpha for your view in Android API-10 and lower.

Download it from github

4 Comments

Sergei · 18 May, 2014 at 15:29

Man, you are awesome! That helped me really much, thank you!

Mahmoud ELshamy · 20 July, 2014 at 23:26

You are really awesome!, thanks very much

Carlos · 3 September, 2014 at 18:38

Thank you man! You helped me alot.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.