Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overlapping Views in Android

Is it possible to have overlapping views in Android? I would like to have an ImageView with a transparent png in the front and another view in the background.

edit:

This is what I have at the moment, the problem is that the image in the imageView is not transparent, the parts that should be transparent are just black.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:orientation="vertical"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:id="@+id/gallerylayout" > <Gallery android:id="@+id/overview"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     />   <ImageView android:id="@+id/navigmaske"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:src="@drawable/navigmask"     />   </RelativeLayout> 

edit:

I got it to work, it was a theme file from another programmer on the team. Just changed this

<item name="android:background">#FF000000</item> 

to this

<item name="android:background">#00000000</item> 
like image 365
Alexander Stolz Avatar asked Jun 07 '09 14:06

Alexander Stolz


People also ask

Which layout is used for overlapping of views in Android?

The easiest way to do it would be to have an AbsoluteLayout and then put the two images where you want them to be. You don't need to do anything special for the transparent png except having it added later to the layout.

What is Z index in Android?

In a FrameLayout , the z-index is defined by the order in which the items are added, for example: <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <ImageView android:layout_width="wrap_content" android:layout_height=" ...

How do I move one view to the top of another Android?

Just in case if you want to place a view on top of a ButtonView then use this; android:elevation="7dp" for the view which needs to be placed on top of the button. Thanks. It worked, but also worked with 1dp up for me.


2 Answers

Android handles transparency across views and drawables (including PNG images) natively, so the scenario you describe (a partially transparent ImageView in front of a Gallery) is certainly possible.

If you're having problems it may be related to either the layout or your image. I've replicated the layout you describe and successfully achieved the effect you're after. Here's the exact layout I used.

<RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"   android:id="@+id/gallerylayout"   android:layout_width="fill_parent"   android:layout_height="fill_parent">   <Gallery     android:id="@+id/overview"     android:layout_width="fill_parent"      android:layout_height="wrap_content"   />   <ImageView     android:id="@+id/navigmaske"     android:background="#0000"           android:src="@drawable/navigmask"     android:scaleType="fitXY"     android:layout_alignTop="@id/overview"     android:layout_alignBottom="@id/overview"     android:layout_width="fill_parent"      android:layout_height="wrap_content"   /> </RelativeLayout> 

Note that I've changed the parent RelativeLayout to a height and width of fill_parent as is generally what you want for a main Activity. Then I've aligned the top and bottom of the ImageView to the top and bottom of the Gallery to ensure it's centered in front of it.

I've also explicitly set the background of the ImageView to be transparent.

As for the image drawable itself, if you put the PNG file somewhere for me to look at I can use it in my project and see if it's responsible.

like image 130
Reto Meier Avatar answered Sep 17 '22 23:09

Reto Meier


Also, take a look at FrameLayout, that's how the Camera's Gallery application implements the Zoom buttons overlay.

like image 39
reflog Avatar answered Sep 21 '22 23:09

reflog