FET FET - 3 months ago 9
Android Question

How to add a gray overlay on any View in a layout

I was wondering how to add a gray overlay to everything inside of a layout file in order to see everything inside the layout, but with this gray transparent overlay on.

I tried to make a

View
and set to it a
background
like
#80000000
, but it looks like this
View
takes all the space (I'm using a
LinearLayout
in fact).

Instead I want to have this overlay above all the other Views, but still want to be able to see what's behind.

Answer

One of the ways you can achieve the effect is by using a <FrameLayout> container as your root. Within it you can add your current layout and the view with a background together. Here, the view should cover the entire screen. So, your layout will look something like:

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

    <!-- Your current layout here -->

    <!-- View to cover your screen -->
    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/black" />

</FrameLayout>
Comments