11

Hi I'm trying to set alpha value for my relative layout but, i am getting error how to solve this help me.....

I have three layout in my xml layout 1st layout using for background 2nd layout using for header 3rd layout using for footer. I wish to set alpha value 2 & 3rd layout so i am trying many ways still i have no idea please tell how to set alpha value

xml code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:orientation="vertical" 
    android:background="@drawable/blue">

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="60px"
        android:orientation="horizontal"
        android:layout_alignParentTop="true"
        android:background="@drawable/gradient_black"
        android:id="@+id/ttest">
       <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

        />

        <TextView
            android:id="@+id/header_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="6dp"
            android:layout_toRightOf="@+id/imageView1"
            android:text="settings"
            android:textColor="@color/white"
            android:textSize="20sp" />

    </RelativeLayout>

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="55px"

        android:background="@drawable/gradient_black"
        android:gravity="bottom"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true" >
  <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ssss"
        />  

    </RelativeLayout>

</RelativeLayout>

code:

public class DesignActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        int width = getWindowManager().getDefaultDisplay().getWidth();
        int height = getWindowManager().getDefaultDisplay().getHeight();


        ImageView imgHead = (ImageView)findViewById(R.id.imageView1);

        ImageView imgbottom = (ImageView)findViewById(R.id.imageView2);


        imgbottom.setImageResource(R.drawable.back);
        imgbottom.setLayoutParams(new RelativeLayout.LayoutParams(width/8, height/8));


        imgHead.setImageResource(R.drawable.b);
        imgHead.setLayoutParams(new RelativeLayout.LayoutParams(width/8, height/8));
     //   RelativeLayout relative = (RelativeLayout)findViewById(R.id.ttest);

    }
}
balaji
  • 1,555
  • 5
  • 26
  • 49

2 Answers2

13

RelativeLayout rl; ...
rl.setAlpha(0.5F);

<RelativeLayout
    android:id="@+id/rl"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:background="@drawable/bg"
    android:alpha="0.5">
SMD
  • 404
  • 1
  • 4
  • 9
  • rl.setAlpha needs API >= 11 – Darpan Jun 23 '15 at 04:56
  • This makes the entire layout transparent, not just the background image, it seems. I had some view elements like buttons and textviews that also took on this alpha value when I added it to the root RelativeLayout. – The Unknown Dev Apr 04 '17 at 23:16
-1

In these cases I generally tend to want to set the colour and alpha at the same time, so I just use rl.setBackgroundColor(0xAACCCCCC); where A is alpha value and C is colour, in hex format.

e.g: rl.setBackgroundColor(0x88000000); for 0.5 transparent black background.

or in XML: android:background="#88000000"

luke-trimby
  • 1,573
  • 1
  • 9
  • 14