How to make a LayerDrawable (layer-list item) invisible on Android?
Asked Answered
W

2

7

I have defined a layer-list with a couple of items on an xml file. The items are displayed o.k. I want every five second or so one of the layers to become invisible.

It works o.k. for a simple textview for example but not for the Layer inside the LayerDrawable

final private Runnable runnable = new Runnable() {
    public void run() {
        LayerDrawable myDrawable= (LayerDrawable)getResources().getDrawable(R.drawable.all_layers);
        Drawable layer =  myDrawable.findDrawableByLayerId(R.id.interesting_layer);
        if (layer.isVisible()==true)
        {
            layer.setVisible(false, false);
        }
        else
        {
            layer.setVisible(true, false);
        }
        TextView txt = (TextView) findViewById(R.id.txtTest);
        if (txt.getVisibility()==0)
        {
            txt.setVisibility(4);
        }
        else
        {
            txt.setVisibility(0);
        }
        handler.postDelayed(this, 5000);
    }
};

Do I try to get the Id of the layer in a wrong way (I found it from here...)? Thanks in advance!

Winny answered 9/3, 2011 at 16:15 Comment(0)
O
21

I did that playing with the alpha of the layer. This code will make your layer disappear:

layer.setAlpha(0);

And then you can display it again with:

layer.setAlpha(255);

Hope this helps.

Overspread answered 25/6, 2011 at 9:24 Comment(4)
yeah, it totally helps but why setVisible(false, false) not working? In my specific case i need to hide one of the drawables, so i'm trying to execute layerDrawable.getDrawable(1).setVisible(false, false); - why this isn't working?Calcium
Odd. Because of this, I've reported here: issuetracker.google.com/issues/127538945Haifa
I don't know why, but setAlpha to 0 in my case leaves a gray background =(Malediction
@yeyeyerman, Thanks, this one helped me in 2024. Still layer.setVisible doesn't workSeveral
P
0

Setting the background tint as transparent gave me the desired output in my case

layer.setTint(Color.TRANSPARENT);

I tried using the isvisible() function however it wasn't working for some reason

Perreault answered 26/6, 2023 at 19:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.