4

I am using the following code to set the alpha value of a layout:

<RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="54dp"
        android:alpha="0.85"
        android:background="@color/label_background"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <LinearLayout
            android:id="@+id/account"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true" >

            <Button
                android:id="@+id/accountButton"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_margin="12dp"
                android:background="@drawable/settings" />
        </LinearLayout>

        <ImageView
            android:layout_width="1dp"
            android:layout_height="fill_parent"
            android:layout_toRightOf="@id/account"
            android:scaleType="fitXY"
            android:src="@android:drawable/divider_horizontal_dim_dark" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:gravity="center_vertical"
            android:text="@string/pick_up"
            android:textColor="@color/white"
            android:textSize="22dp"
            android:textStyle="bold"
            custom:customFont="Roboto-Regular.ttf" />
    </RelativeLayout>

The problem that i am facing is that setting an alpha value to the whole relative layout will make its children(the TextView and the Button) transparent also. However, I want the button and the text to be solid colors and not transparent. I tried setting an alpha value of 1 to the TextView and the Button but it didn't help. How can I correct this?

Ankush
  • 6,767
  • 8
  • 30
  • 42
  • Is it necessary to set alpha for bg by xml? Here's an example how to set it programatically http://stackoverflow.com/questions/4968883/opacity-on-a-background-drawable-image-in-view-using-xml-layout – sandrstar Nov 20 '12 at 08:36
  • doing it programmatically won't make a difference. The alpha will still be set to the whole layout and its children – Ankush Nov 20 '12 at 09:01

1 Answers1

2

I was able to to do it in the end by using the following code:

<FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:alpha="0.85"
            android:background="@color/label_background" />

        <RelativeLayout
            android:id="@+id/relativeLayout1"
            android:layout_width="fill_parent"
            android:layout_height="54dp"
            android:gravity="center_vertical"
            android:orientation="horizontal" >

            <LinearLayout
                android:id="@+id/account"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true" >

                <Button
                    android:id="@+id/accountButton"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_margin="12dp"
                    android:background="@drawable/settings" />
            </LinearLayout>

            <ImageView
                android:layout_width="1dp"
                android:layout_height="fill_parent"
                android:layout_toRightOf="@id/account"
                android:scaleType="fitXY"
                android:src="@android:drawable/divider_horizontal_dim_dark" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:gravity="center_vertical"
                android:text="@string/pick_up"
                android:textColor="@color/white"
                android:textSize="22dp"
                android:textStyle="bold"
                custom:customFont="Roboto-Regular.ttf" />
        </RelativeLayout>
    </FrameLayout>

basically, i surrounded the relativeLayout with FrameLayout and set the alpha value and the background in the FrameLayout itself.

Ankush
  • 6,767
  • 8
  • 30
  • 42