How to set a translucent background for a layout programmatically?

android, android-layout

Solution

use `setBackgroundResource` or `setBackgroundColor`. I think first is pretty simple.

Second one takes an `int` as an argument. So, just convert your hex color (for example `#55000000`) into decimal and it will work as well.

However, it's better to use `setBackgroundResource`, because in this case you store your colors in separate file:

setBackgroundResource(R.color.mycolor);
setBackgroundResource(android.R.color.transparent);
//etc

Problem

My application has a class that extends Relative Layout. I would like to know if there is any way in which I can make the background of this class translucent programmatically. Since its not an activity I cannot set the style attribute in the Manifest file to "Translucent" and I cannot use the setAlpha() method too since the setAlpha() method is applicable from API Level 11 onwards and my application has an API Level of 11 and due to certain restrictions I cannot change it to API Level 11. Could you please guide me in this issue? Thank you.

Original source