Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I display one view as an overlay of another?

I have two views that take the whole screen and I want to display both views at the same time, one on top of the other. My layout looks like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:orientation="vertical">      <WebView          android:id="@+id/webview"         android:layout_width="fill_parent"         android:layout_height="fill_parent"     />      <org.example.myCustomView         xmlns:android="http://schemas.android.com/apk/res/android"         android:layout_width="fill_parent"         android:layout_height="fill_parent"     />  </LinearLayout> 

Note that myCustomView uses onDraw (this method last statement is invalidate()) to draw custom graphics. The problem I am getting is that it only displays myCustomView, WebView is hidden. I tried to change the background colour of mycustomView to transparent but this makes no difference.

I would also like to have the ability to make myCustomView as an overlay on WebView or vice versa.

like image 921
ace Avatar asked Feb 04 '11 18:02

ace


People also ask

What is overlay view?

An overlay is an extra layer that sits on top of a View (the "host view") which is drawn after all other content in that view (including children, if the view is a ViewGroup).

How to overlay View android?

Simply use RelativeLayout or FrameLayout . The last child view will overlay everything else. Android supports a pattern which Cocoa Touch SDK doesn't: Layout management.

What is FrameLayout?

FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other.


1 Answers

Use a RelativeLayout for starters.

You could use the ViewGroup.addView(View child, int index, ViewGroup.LayoutParams params) or a variant along with ViewGroup.removeView(View view) or ViewGroup.removeViewAt(int index).

This would obviously require you to inflate the views manually using LayoutInflater but you could keep a global reference to each after inflating and just flip between the two.

like image 189
techiServices Avatar answered Sep 18 '22 14:09

techiServices