Android: How to specify the opacity of a <layer-list>?
Asked Answered
T

3

7

I want my LayerList to have an opacity of 80 so I wrote:

<layer-list
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:opacity="80">

But I get:

Error: Integer types not allowed (at 'opacity' with value '80')

How can I specify the opacity?

Teddi answered 28/1, 2013 at 8:58 Comment(1)
If you can do it that way - what I'm not sure for layer-list. This will be called android:alphaCthrine
R
-1

android:opacity supports only translucent|opaque|transparent values in layer-list.

By the way what is your requirement?

Updated-Answer: To set opacity/less-visible as 80%, you need to set to your view

android:alpha="0.8"

Rosebud answered 28/1, 2013 at 9:22 Comment(3)
Requirement: make the image less visible (it is an image in the background, just for style)Teddi
@NicolasRaoul Sorry didn't get back to you. As per my understanding, you are using the layerList that you have created as a background drawable in an ImageView or some other view using @drawable/layer_list. To set opacity/ image less visible as 80%, you need to set android:alpha="0.8"Rosebud
(I switched to another solution meanwhile, so I won't test this soon, but I accept as it is the only answer)Teddi
L
1

To give a translucent effect, say 50% opacity, use:

Drawable d = new ColorDrawable(Color.BLACK);
d.setAlpha(130);
mDialog.getWindow().setBackgroundDrawable(d);

'130' can be changed (0-255) to acheive desired opacity.

Lunnete answered 13/2, 2014 at 13:33 Comment(0)
A
1

I think the question asks about setting the opacity of individual items inside a <layer-list>, and not the whole image programmatically.

If you know the color already, Use a combine hex RGBA code, for e.g. :

<solid android:color="#80FF0000" />

Sets the color to red (0xFF0000) with 0x80 opacity.

Same thing applies to few other items. e.g. stroke

<stroke android:color="#6000FF00" android:width="20dp" />

This creates a 20dp thick green stroke of 0x60 opacity.

Futher, if you want to do it pro grammatically, i.e. modify the alpha of a specific item and not the whole drawable, use a new resource id (android:id="@+id/...") for the item and access it . I haven't happened to verify this part though.

Acinus answered 24/12, 2018 at 15:47 Comment(0)
R
-1

android:opacity supports only translucent|opaque|transparent values in layer-list.

By the way what is your requirement?

Updated-Answer: To set opacity/less-visible as 80%, you need to set to your view

android:alpha="0.8"

Rosebud answered 28/1, 2013 at 9:22 Comment(3)
Requirement: make the image less visible (it is an image in the background, just for style)Teddi
@NicolasRaoul Sorry didn't get back to you. As per my understanding, you are using the layerList that you have created as a background drawable in an ImageView or some other view using @drawable/layer_list. To set opacity/ image less visible as 80%, you need to set android:alpha="0.8"Rosebud
(I switched to another solution meanwhile, so I won't test this soon, but I accept as it is the only answer)Teddi

© 2022 - 2024 — McMap. All rights reserved.