Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opacity on a background Drawable image in View (using XML Layout)

I was just wondering if there was a way to change the opacity of the background image for a View (ie. TextView, etc.).

I know that I can set the background image like this:

android:background="@drawable/my_drawable_image" 

Or I can set a specific background colour with an alpha setting like this:

android:background="#10f7f7f7" 

Is there a way I can control the opacity (set the alpha) if I'm setting the background as a drawable image? And I want to do this in the XML Layout. I already know that I could grab the Drawable object and programmatically set the alpha, but I want to see if I can do it in the layout.

like image 659
xil3 Avatar asked Feb 11 '11 12:02

xil3


People also ask

How do you change opacity in XML?

This example demonstrates how to Set opacity for View in Android . Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I change opacity of an image in android?

Here is a simple function to change the opacity of a view in android. Opacity is called alpha in android. so setAlpha(int) will do the job. ImageView img = (ImageView)findViewById(R.


1 Answers

I ended up just going with the programmatical solution, since it doesn't look like it can be done via the XML layouts.

Drawable rightArrow = getResources().getDrawable(R.drawable.green_arrow_right_small);  // setting the opacity (alpha) rightArrow.setAlpha(10);  // setting the images on the ImageViews rightImage.setImageDrawable(rightArrow); 
like image 69
xil3 Avatar answered Sep 20 '22 08:09

xil3