So you have some color like and you want to add transparency to it. Rather than storing that value inside the colors.xml file, you can do so programmatically.

If you are using the support library, you can use ColorUtils.setAlphaComponent(color, alpha) method.

If you want using basic Java, do it like this: (color & 0x00ffffff) | (alpha << 24). Remember, the color value is not R.color.mycolor. I use the Compat library all the time so I convert the R.color.mycolor variable to ContextCompat.getColor(context, R.color.mycolor) then pass that to the formula above or the setAlphaComponent() parameter.

One might think the quickest and easiest way to hide a Surface object in is to set its opacity to transparent.

While true, you will also have to set its other properties like text color and such which is a hassle. Okay, so if you do not find that a hassle, there is another problem.

All input events like mouse clicks will be absorbed by this surface. So if you intentionally wanted to hide the Surface because the you want the other Surface below it to receive events, it will not work.

What I did was to make use of the Modifier that was used to add the Surface to the View. It has a setOrigin method that you can take advantage of to move this Surface object away from the user’s screen.

Say, setOrigin(0.5, 0.5) moves the Surface object to the center of the screen. You can do setOrigin(-0.5, -0.5) to move it out of the user’s screen making the user think that you actually hid the Surface object when in fact, you just moved it.

To show the Surface object back, just set the values to non-negative. That’s it!

Simply set the property backgroundColor with an rgba() value. RGBA means RGB (red, green, blue) + Alpha where you set the opacity.

If you want it to be invisible, set it to 0, else 1. If you want it to be semi transparent then set values between 0 and 1.

This is an example on how to set the background of a Surface to transparent.

And this is how to set it back to normal.

Related Posts Plugin for WordPress, Blogger...