mugimugi
mugimugi

Reputation: 446

image.setAlpha on Android lollipop not work

After testing my app on Android 5.0, I noticed that image.setAlpha() is not working on this Android version.

I tried with image.setImagealpha() function, but it returns this error: "The method setImageAlpha(int) is undefined for the type Drawable"

The API level that I´m using on my app is 8

What can I do?

Upvotes: 2

Views: 3615

Answers (2)

Hitesh Sahu
Hitesh Sahu

Reputation: 45150

Update 2019:

With kotlin now we can do it like this:

imageView.post {imageView.alpha = 1.0f}

I have used View.post so that it get updated on immediate next UI updation cycle.

Without posting on UI thread sometimes setting alpha of TextViews doesn't reflect on UI as discussed here

Upvotes: 2

ChallengeAccepted
ChallengeAccepted

Reputation: 1704

ImageView has the method setAlpha(float) after API 11. Before API 11 it uses setAlpha(int). Since you want to support API 8 and above, you have to specify the different states. So to resolve this, use the following code:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB){
    //For API 11 and above use a float such as 0.54.(54% transparency)        
    imageView.setAlpha(float);
}
else
    //For API Below 11 use an int such as 54.
    imageView.setAlpha(int);

Upvotes: 1

Related Questions