Marcel
Marcel

Reputation: 162

How to create a See-Through View in Android XML-Layout

My Android XML-Layout looks like this

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:gravity="center"
          android:background="?android:colorBackground"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

        <SomeView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:background="@android:color/transparent"/>


    </LinearLayout>

What I need is a 'See-Through' hole in my Background LinearLayout at the size of my small View. Like in the following Picture:

enter image description here

Is this possible? How?

Upvotes: 2

Views: 1310

Answers (3)

Neh
Neh

Reputation: 452

If you wish a see-through background for the view, what can be done is :-

  • Layout 1 : Take a parent layout filling the background as - android:layout_width="match_parent" android:layout_height="match_parent" : Say red color
  • Layout 2 : Now take another layout filling whatever size of the screen is needed : Say blue color
  • View : You can place your view [the see through one] in this layout and layout_width=20px, layout_height = 20px and background color should be the same as that of Layout1 that is red. This would now look like a see-through hole in the blue background.

Hope that this helps.

Example :

<LinearLayout
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent" android:background="@android:color/holo_red_dark">
    <LinearLayout
        android:orientation="vertical"
        android:gravity="center"
        android:background="@android:color/holo_blue_bright"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="30dp">

        <View
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@android:color/holo_red_dark"/>


    </LinearLayout>

</LinearLayout>

Upvotes: 0

Look up the Alpha channel - I think that's basically what you're looking for here.

Upvotes: 0

Charuka Silva
Charuka Silva

Reputation: 13153

use transparent background in views that you want to make transparent so if there is any view behind it it will display

eg:

 android:background="#00000000"
  • Normal opaque black hex- "#000000"
  • Fully transparent - "#00000000"
  • Fully opaque - "#FF000000"
  • 50% transparent - "#80000000"

Upvotes: 1

Related Questions