Reputation: 3403
I am using an image as the background of a linear layout in my android program.I need to set opacity for this.Can anyone tell me how can I do this?..The image that is translucent doesnt show the opacity,doesn't know the reason though. Thanks in advance for your valuable comments
Upvotes: 2
Views: 5087
Reputation: 1447
If you set the background of the LinearLayout programatically this shouldn't be any problem.
What you are looking for is the Drawable.setAlpha(int alpha)
method. From a personal project:
ImageView image = (ImageView) row.findViewById(R.id.list_icon);
image.setImageResource(R.id.something);
image.setAlpha(110);
Not exacly the same, but maybe you get the point.
You are using a drawable as the background of your layout. The challenge here is to get the variable representing your drawable. This is done here:
In the activity to the layout:
Resources res = getResources();
Drawable background = res.getDrawable(R.drawable.*the id*);
// The layout which are to have the background:
LinearLayout layout = ((LinearLayout) findViewById(R.id.*you get it*));
// Now that we have the layout and the background, we ajust the opacity
// of the background, and sets it as the background for the layout
background.setAlpha( *a number* );
layout.setBackgroundDrawable(background);
And it should work. At least it did work for me.
Upvotes: 6