2

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?

mugimugi
  • 446
  • 7
  • 19
  • ImageView has the method setAlpha(float) after API 11. Before API 11 it uses setAlpha(int). Are you giving it a float value instead of an int? For 80% transparency you would do imageView.setAlpha(0.8); – ChallengeAccepted Nov 14 '14 at 23:17
  • I am giving an int value. I want the app to work both API 8 and Android 5.0 – mugimugi Nov 18 '14 at 22:43
  • I have posted a solution that should work. Please mark it as the correct answer if it did resolve your issue. Happy Coding! – ChallengeAccepted Nov 25 '14 at 21:41

2 Answers2

2

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

Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
1

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);
ChallengeAccepted
  • 1,684
  • 1
  • 16
  • 25
  • First of all, thanks for doing that code, but it didn´t work :s – mugimugi Nov 27 '14 at 22:25
  • I did some more researching around and found this. See if this works and incorporate it with my code for checking the API level. Good Luck! http://stackoverflow.com/a/4931349/1371041 – ChallengeAccepted Nov 30 '14 at 05:39