1

I want to change my Android LinearLayout background transparency. In here I tried to change the android:alpha="0.4" value in activity_view_full_screen_image.xml. But layout content such as PhotoView transparency is changed instead of layout transparency.

Here is my code:

ViewFullScreenImage.java

import android.graphics.Color;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;

import com.github.chrisbanes.photoview.PhotoView;
import com.squareup.picasso.Picasso;

import java.io.InputStream;

public class ViewFullScreenImage extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_full_screen_image);
        loadImage();
    }

    public void loadImage(){

        Uri imageUrl = Uri.parse("http://www.ucdmc.ucdavis.edu/hr/images/body/Health-Welness-Businesses.jpg");

        PhotoView photoView = (PhotoView) findViewById(R.id.photo_view);
        Picasso.with(this).load(imageUrl).into(photoView);
    }
}

and

activity_full_screen_image.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_view_full_screen_image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:alpha="0.4"
    tools:context="com.example.theace.fullscreen.ViewFullScreenImage">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/my_layout"
        android:orientation="vertical"
        >

        <com.github.chrisbanes.photoview.PhotoView
            android:id="@+id/photo_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>


    </LinearLayout>



</LinearLayout>
halfer
  • 19,824
  • 17
  • 99
  • 186
Nuwan Withanage
  • 393
  • 7
  • 19

3 Answers3

4

android:alpha="0.4"

Do not set alpha of the whole view as you do now with android:alpha but instead set its background to be i.e. semi transparent color:

android:background="#AARRGGBB"

where AA stands for alpha of the color (with FF being fully opaque, and 00 being fully transparent):

android:background="#80FF0000"

will give 50% transparent red.

Docs: https://developer.android.com/guide/topics/resources/more-resources.html#Color

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
1

Set the background color to something like this

android:background="#88FFFFFF"

Where the color is in AARRGGBB format. The first 2 digits can be used to control the alpha of the background.

Ajil O.
  • 6,562
  • 5
  • 40
  • 72
1

It depends on android:background namespace in your parent layout. Instead of setting android:alpha="0.4" in your layout, set background color !

For example, android:background="#40ffffff"

the front 2 digits are alpha value.

John Lee
  • 80
  • 10