4

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.

user1777907
  • 1,355
  • 4
  • 12
  • 28

5 Answers5

16

I actually figured it out! The colors are AARRGGBB, so modifying the alpha channel (AA) only affects the current background and not the children! If there is another solution, I am happy to hear it. Thanks!

user1777907
  • 1,355
  • 4
  • 12
  • 28
  • 1
    Yeah, this is the way to do it. This response gives a complete how-to: http://stackoverflow.com/questions/15852122/hex-transparency-in-colors – B-Money Sep 04 '14 at 16:08
10

You can solve your problem with this solution. It works perfect.

parentView.getBackground().setAlpha(128); //your parent view's visibility is now %50 and child view's visibility remains same. 
Community
  • 1
  • 1
ACengiz
  • 1,285
  • 17
  • 23
1

To prevent a child view from being affected by it's parent background ...

Truth: A non-alpha-255 'color' is not actually a color - it is merely a tint!

Thus: the perceived appearance of a child view background is either:
(a) The exact color specified by child.setBackgroundColor() when that color has alpha-255 or:
(b) A composite of child.setBackgroundColor() and the parent background otherwise.

To absolutely control the child background (with utter disregard the parent) you must therefore construct a third color which will be a composite of your chosen tint and your chosen alpha-255 background.

You must nominate a background! (By definition, a tint can only be rendered against a background. If not explicitly specified the theme background will eventually come into play.)

This code took months to find and is giving perfect results.

childView.setBackgroundColor(ColorUtils.compositeColors(yourTint, yourBackground);

See my answer to my own question here.

Community
  • 1
  • 1
Bad Loser
  • 3,065
  • 1
  • 19
  • 31
1

If you want to set the alpha only to the parent parent layout and don't want it to be translated to its child views. Use Hexadecimal color with transparency code added to it.

Ex- Let say the background color for your Linearlayout is #FF0022 and you want to set it's opacity to 82% then add D1 to your hex code and use #D1FF0022 hex code for your viewgroup background where D1 is the parameter for the opacity of 82%

complete list of transparency code can be found here

rahul
  • 208
  • 1
  • 7
0

This may be late answer

But Try this below code i hope u get what u need

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/aa"    //Your own Image in root element
android:orientation="vertical" >

  <RelativeLayout
    android:id="@+id/RLMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#CCFFFFFF" >   //Your Alpha Value

    <ImageView
        android:id="@+id/iv1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="86dp"
        android:src="#FF0000" />        //Your Child Image

</RelativeLayout>