Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a translucent background for a layout programmatically?

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.

like image 491
Ingrid Cooper Avatar asked Aug 21 '12 05:08

Ingrid Cooper


1 Answers

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
like image 134
Dmitry Zaytsev Avatar answered Nov 10 '22 22:11

Dmitry Zaytsev