Search code examples
androidviewvisibilitylayer

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


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!


Solution

  • 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.