Search code examples
androidviewxml-drawable

Is there a way to set alpha attribute to child views as well as to parent view?


I am developing app where I have set custom drawable to main layout with alpha attribute. But it sets alpha to whole view. and I wanted to set alpha to the only main layout. so that the text which will displayed normally without alpha effect. Present Output

The alpha effect is taken by whole view.

<android.support.constraint.ConstraintLayout
        android:id="@+id/containerLinear"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/gradient_with_bottom_curve"
        android:orientation="horizontal"
        android:alpha="0.4"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">
        <ScrollView
            android:id="@+id/mainScroll"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <LinearLayout
                android:id="@+id/scrollLinear"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <RelativeLayout
                    android:id="@+id/mainRelative"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                    <TextView
                        android:id="@+id/txtHeading"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Business Name"
                        android:gravity="center"
                        android:layout_marginTop="10dp"
                        android:padding="5dp"
                        android:textStyle="bold"
                        android:textSize="20sp"
                        android:textColor="@color/logo_orange"/>
                    <RelativeLayout
                        android:id="@+id/paraOneRelative"
                        android:layout_below="@+id/txtHeading"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
                        <TextView
                            android:id="@+id/txtParaOneBold"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Some write up"
                            android:gravity="start"
                            android:layout_marginTop="10dp"
                            android:padding="5dp"
                            android:textStyle="normal"
                            android:textSize="12sp"
                            android:textColor="@color/black"/>
                    </RelativeLayout>
                </RelativeLayout>
            </LinearLayout>
        </ScrollView>
    </android.support.constraint.ConstraintLayout>

Solution

  • You could use FrameLayout as the base instead of ConstraintLayout and layer the ScrollView on top of a View (which has the translucent background). Something along the following lines should work,

    <FrameLayout
        android:id="@+id/containerLinear"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    
        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:alpha="0.4"
            android:background="@drawable/gradient_with_bottom_curve" />
    
        <ScrollView
            android:id="@+id/mainScroll"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <LinearLayout
                android:id="@+id/scrollLinear"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <RelativeLayout
                    android:id="@+id/mainRelative"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                    <TextView
                        android:id="@+id/txtHeading"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Business Name"
                        android:gravity="center"
                        android:layout_marginTop="10dp"
                        android:padding="5dp"
                        android:textStyle="bold"
                        android:textSize="20sp"
                        android:textColor="@color/logo_orange"/>
                    <RelativeLayout
                        android:id="@+id/paraOneRelative"
                        android:layout_below="@+id/txtHeading"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
                        <TextView
                            android:id="@+id/txtParaOneBold"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Some write up"
                            android:gravity="start"
                            android:layout_marginTop="10dp"
                            android:padding="5dp"
                            android:textStyle="normal"
                            android:textSize="12sp"
                            android:textColor="@color/black"/>
                    </RelativeLayout>
                </RelativeLayout>
            </LinearLayout>
        </ScrollView>
    </FrameLayout>