Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add semi-transparent overlay to activity backround at runtime

I have an app that i set the background to an image from xml. I get the view by calling

setContentView(R.)

. How can i place a semi-transparent overlay on this background at runtime depending on a condition. I would like to have a red overlay with its alpha set to 50%.

I have tried creating a seperate xml file with the view duplicated and a different image/overlay but its messy as i have to inflate all the buttons/textview when i use the new view.

thanks matt

[edit1]

<?xml version="1.0" encoding="utf-8"?>

 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<LinearLayout android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/carefreebgscaledlighting"
    android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">


    <TextView
        android:id="@+id/textviewcompanyname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#003F87" />

    <TextView
        android:id="@+id/textViewYouAreSignedIn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_alignParentTop="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#003F87"
         />

          <TextView
        android:id="@+id/textViewUnsentTransactions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_below="@id/textViewYouAreSignedIn"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#003F87"
         />

[edit2]

<?xml version="1.0" encoding="utf-8"?>


 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >


     <RelativeLayout
        android:id="@+id/transparentOverlay"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/red_transparent" >


<LinearLayout android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/carefreebgscaledlighting"
    android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">




    <TextView
        android:id="@+id/textviewcompanyname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#003F87" />

    <TextView
        android:id="@+id/textViewYouAreSignedIn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_alignParentTop="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#003F87"
         />

[edit3]

<?xml version="1.0" encoding="utf-8"?>


 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >



<LinearLayout 
    android:id="@+id/ll1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/carefreebgscaledlighting"
    android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">




    <TextView
        android:id="@+id/textviewcompanyname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#003F87" />

.

if(status.equalsIgnoreCase(IN)){

                youAreSignedInAt.setText("You are signed in at " + name);
                LinearLayout layout =(LinearLayout)findViewById(R.id.ll1);
                layout.setBackgroundResource(R.drawable.carefreebgscaledlightingred);
            }else{

                youAreSignedInAt.setText("You are not signed in");
                LinearLayout layout =(LinearLayout)findViewById(R.id.ll1);
                layout.setBackgroundResource(R.drawable.carefreebgscaledlighting);
            }
like image 550
turtleboy Avatar asked Sep 27 '13 15:09

turtleboy


1 Answers

Off the top of my head, you could declare the background color including alpha component in a resource file (i.e. colors.xml)

<color name="red_transparent">#66FF0000</color>

And then put a view (i.e. RelativeLayout), on top of the rest of the views.-

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- All your views -->

    <!-- Transparent layer -->   
    <RelativeLayout
        android:id="@+id/transparentOverlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/red_transparent" />

</RelativeLayout>

All you need to do is changing visibility of R.id.transparentOverlay as you desire.

like image 51
ssantos Avatar answered Sep 27 '22 21:09

ssantos