42

I have put an image in as the background of my android application with the following line of code:

 android:background="@drawable/background"

I now want to make it transparent by 40% but how is this possible within the xml file? My exm file is shown below:

<LinearLayout 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"
android:background="@drawable/background"
android:alpha="0.6"
android:orientation="vertical"
android:padding="30dp"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SecondActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="5dp"
    android:text="Welcome"
    android:textColor="#292421"
    android:textSize="17sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/TextView01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:text="This Android application is used to track and record the movements of a person throughout the university of ulster, Magee Campus. The tracking phase of the application can only be initiated whilst within the campus and off campus tracking is unavailable at this moment in time."
    android:textColor="#292421"
    android:textSize="17sp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="85dp"
        android:layout_weight="1"
        android:background="#E6E6FA"
        android:onClick="tracking"
        android:text="@string/tracking_service" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:padding="20dp" >

    </LinearLayout>

    <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="85dp"
        android:layout_weight="1"
        android:background="#E6E6FA"
        android:onClick="help"
        android:text="Help" />

</LinearLayout>

user3393962
  • 433
  • 1
  • 4
  • 6

9 Answers9

49

You can set the alpha property:

android:alpha="0.4"

via code :

yourView.setAlpha(0.5f);

only the background :

yourView.getBackground().setAlpha(120);  // here the value is an integer not float
Decoy
  • 1,598
  • 12
  • 19
  • 2
    I have found a problem with using this as my text on this page is also faded which I don't want, is there another way of doing this? – user3393962 Mar 14 '14 at 14:02
  • How do I do this if the background image is embedded into the root constraint layout itself? – isakbob Jul 19 '18 at 20:15
39

If you have set an image as background then

 android:alpha="0.x"

will cause entire screen (child views) to fade. Instead you can make use of

android:backgroundTint="#80FFFFFF"
android:backgroundTintMode="src_over"

to fade only the background image without affecting the child views.

Pramod Garg
  • 2,183
  • 1
  • 24
  • 29
24

It can be done using custom drawable. Its a very old post but thought I should answer, might be helpful for someone.

  1. Create a drawable xml resource with bitmap as the root say bg.xml.
  2. set android:src="@drawable/background" to the image which you want as the background.
  3. set android:alpha="0.4" to any value between 0 to 1 as per the required opacity.
  4. now set the background of the root layout as newly created drawable xml resource i.e., bg.xml. This will only change the opacity of the background image of the layout without effecting opacity of the child elements of the layout.

Resources :
bg.xml

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/background" android:alpha="0.4"/>

This is how your layout will look now :

<LinearLayout 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"
android:background="@drawable/bg"
android:orientation="vertical"
android:padding="30dp"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SecondActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="5dp"
    android:text="Welcome"
    android:textColor="#292421"
    android:textSize="17sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/TextView01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:text="This Android application is used to track and record the movements of a person throughout the university of ulster, Magee Campus. The tracking phase of the application can only be initiated whilst within the campus and off campus tracking is unavailable at this moment in time."
    android:textColor="#292421"
    android:textSize="17sp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="85dp"
        android:layout_weight="1"
        android:background="#E6E6FA"
        android:onClick="tracking"
        android:text="@string/tracking_service" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:padding="20dp" >

    </LinearLayout>

    <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="85dp"
        android:layout_weight="1"
        android:background="#E6E6FA"
        android:onClick="help"
        android:text="Help" />

</LinearLayout>
Monish Kamble
  • 1,478
  • 1
  • 15
  • 28
16

you can create a layer-list resource and set it to background property of your view (layout)

background_image_with_alpha.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <bitmap
            android:alpha="0.2"
            android:src="@drawable/xxxxxx"
            android:gravity="fill">

        </bitmap>
    </item>

</layer-list >
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Pep Vivó
  • 161
  • 1
  • 2
  • This is the correct answer if you don't want opacity to affect elements on the screen, just the background image – Seany242 Jun 05 '18 at 14:37
  • Great way to effect only the background's opacity AND not mess up your code with layout stuff. – TDG Sep 02 '18 at 16:27
3

set in your xml file

android:alpha="0.4"

which will be varies from 0 to 255.

<LinearLayout 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"
 android:alpha="0.6"
 android:orientation="vertical"
 android:padding="30dp"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context=".SecondActivity" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:text="Welcome"
android:textColor="#292421"
android:textSize="17sp"
android:textStyle="bold" />

<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="This Android application is used to track and record the movements of a person throughout the university of ulster, Magee Campus. The tracking phase of the application can only be initiated whilst within the campus and off campus tracking is unavailable at this moment in time."
android:textColor="#292421"
android:textSize="17sp" />

<LinearLayout
 android:background="@drawable/background"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="85dp"
    android:layout_weight="1"
    android:background="#E6E6FA"
    android:onClick="tracking"
    android:text="@string/tracking_service" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:padding="20dp" >

</LinearLayout>

<Button
    android:id="@+id/button2"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="85dp"
    android:layout_weight="1"
    android:background="#E6E6FA"
    android:onClick="help"
    android:text="Help" />

</LinearLayout>
Piyush
  • 18,895
  • 5
  • 32
  • 63
3

You can use alpha property android:alpha="0.5"; (Between 0 and 1 )

Ranjeet Chouhan
  • 686
  • 5
  • 13
2

You can use alpha property (http://developer.android.com/reference/android/view/View.html#attr_android:alpha) for your View:

android:alpha="0.4"
nikis
  • 11,166
  • 2
  • 35
  • 45
2

Try to use Drawable.setAlpha();

View backgroundImage = findViewById(R.id.background_image);
Drawable background = backgroundImage.getBackground();
background.setAlpha(80);
0

just use this line in your .xml file android:background="#ddxxxxxx" replace the xxxxxx with any color you want. refer https://alvinalexander.com/android/how-background-image-behind-translucent-opaque-textview for more details.

Atul Giri
  • 11
  • 1