Set alpha-transparency only to layout and not to it's children

Alin picture Alin · Mar 13, 2013 · Viewed 36.7k times · Source

I've found this question and I have almost the same problem. How could I apply alpha only to a relative layout and not to it's children? Could anyone help?

I have a rectangular LinearLayout that has some margin, some round corners and an alpha value of 0.3. Inside this layout I have 4 different layouts as I display different images in different positions. My issue is that although the primary layout is 0.3, I want my child to be fully visible, or not affected by it's parent alpha, and I am wondering how I can please do that please? I have tried setting alpha=1 on the children layouts but it did not work. Setting it to 0 does make the children layout disappear though, so it seems I can reduce below 0.3 but not anything above the parent. Is that a bug or am I doing it wrong please? Thank you.

Answer

stinepike picture stinepike · Mar 14, 2013

Instead of setting the alpha of the parent you can use FrameLayout and set a background image first and set the alpha of that child. For example

Instead of using this

<LinearLayout 
        android:background="@drawable/background" 
        android:alpha="0.3" >
    <LinearLayout>
        <Button />
        <Button />
    </LinearLayout>     
</LinearLayout >

Use this one

<FrameLayout>
    <ImageView
        android:background="@drawable/background" 
        android:alpha="0.3" />
    <LinearLayout>
        <Button />
        <Button />
    </LinearLayout>     
</FrameLayout>