Search code examples
androidtransparency

Android Image transparency - alternative for deprecated setAlpha for API < 11


I want to set transparency on background image. I tried android:alpha in xml, but it does nothing. I use setAlpha(int alpha), and works good, but it is deprecated, so I want another solution (because it might be for example removed in future). There is setImageAlpha and setAlpha(float alpha), but these are not available in api lvl 10 (and that is my target). So my question is: What is the replacement for setAlpha(int alpha) for devices with API 10 or lower (and I just want to mention, that more than 1/4 of all the devices use API 10 or lower). As I checked on stackoverflow, most people still suggest setAlpha(int alpha), but as I said - it is deprecated...


Solution

  • ImageView setAlpha was not deprecated until API level 16. The correct to handle this situation is to continue using it until you can target a minimum SDK level 16, and at that point, simply switch to View setAlpha.

    Deprecation just means to not use in new development if you have an alternative. Since Google has not provided an alternative (i.e., via compatability library) they certainly would not remove it and break untold applications already on your phone.

    Consider yourself lucky you are dealing with ImageView because it has a setAlpha you can use!