Android overlay a view ontop of everything?
Solution 1
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.
Layout for iPhone means to position everything absolute (besides some strech factors). Layout in android means that children will be placed in relation to eachother.
Example (second EditText will completely cover the first one):
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/root_view">
<EditText
android:layout_width="fill_parent"
android:id="@+id/editText1"
android:layout_height="fill_parent">
</EditText>
<EditText
android:layout_width="fill_parent"
android:id="@+id/editText2"
android:layout_height="fill_parent">
<requestFocus></requestFocus>
</EditText>
</FrameLayout>
FrameLayout
is some kind of view stack. Made for special cases.
RelativeLayout
is pretty powerful. You can define rules like View A has to align parent layout bottom, View B has to align A bottom to top, etc
Update based on comment
Usually you set the content with setContentView(R.layout.your_layout)
in onCreate
(it will inflate the layout for you). You can do that manually and call setContentView(inflatedView)
, there's no difference.
The view itself might be a single view (like TextView
) or a complex layout hierarchy (nested layouts, since all layouts are views themselves).
After calling setContentView
your activity knows what its content looks like and you can use (FrameLayout) findViewById(R.id.root_view)
to retrieve any view int this hierarchy (General pattern (ClassOfTheViewWithThisId) findViewById(R.id.declared_id_of_view)
).
Solution 2
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id = "@+id/Everything"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- other actual layout stuff here EVERYTHING HERE -->
</LinearLayout>
<LinearLayout
android:id="@+id/overlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right" >
</LinearLayout>
Now any view you add under LinearLayout with android:id = "@+id/overlay"
will appear as overlay with gravity = right on Linear Layout with android:id="@+id/Everything"
Solution 3
You can use bringToFront
:
View view=findViewById(R.id.btnStartGame);
view.bringToFront();
Solution 4
The best way is ViewOverlay , You can add any drawable as overlay to any view as its overlay since Android JellyBeanMR2(Api 18).
Add mMyDrawable
to mMyView
as its overlay:
mMyDrawable.setBounds(0, 0, mMyView.getMeasuredWidth(), mMyView.getMeasuredHeight())
mMyView.getOverlay().add(mMyDrawable)
Solution 5
I have just made a solution for it. I made a library for this to do that in a reusable way that's why you don't need to recode in your XML. Here is documentation on how to use it in Java and Kotlin. First, initialize it from an activity from where you want to show the overlay-
AppWaterMarkBuilder.doConfigure()
.setAppCompatActivity(MainActivity.this)
.setWatermarkProperty(R.layout.layout_water_mark)
.showWatermarkAfterConfig();
Then you can hide and show it from anywhere in your app -
/* For hiding the watermark*/
AppWaterMarkBuilder.hideWatermark()
/* For showing the watermark*/
AppWaterMarkBuilder.showWatermark()
Gif preview -
Related videos on Youtube
![Thomas Clayson](https://i.stack.imgur.com/XxQ4j.jpg?s=256&g=1)
Thomas Clayson
Updated on July 05, 2022Comments
-
Thomas Clayson almost 2 years
Can you overlay a view on top of everything in android?
In iPhone I would get the new view set its
frame.origin
to (0,0) and its width and height to the width and height ofself.view
. Adding it toself.view
would then cause it to act as an overlay, covering the content behind (or if it had a transparent background then showing the view behind).Is there a similar technique in android? I realise that the views are slightly different (there are three types (or more...) relativelayout, linearlayout and framelayout) but is there any way to just overlay a view on top of everything indiscriminately?
-
Thomas Clayson over 12 yearsAh thanks for the example. So I'm guessing in an activity the content view is probably a framelayout? If I use a
layoutinflater
to grab arelativelayout
from a layout xml file how can I add it to the content view? Is there a way to get a reference to the content view? Thanks -
Knickedi over 12 yearsI updated my answer, so it explains what the content of an activity is and how to interact with it.
-
Thomas Clayson over 12 yearsBrilliant. Thank you very much. :)
-
class stacker about 11 yearsI'd like to point out that while I prefer
FrameLayout
due to its lightweight nature, it fails to propagate the maximum measured height (aka its own resulting height) to its children in case ofmatch_parent
height, see here. Therefore, when supporting Android 2 and working withmatch_parent
,RelativeLayout
is the only working option; for an example see here. -
ray over 9 yearsJFYI: At Android 4.4.4 for me it was enough to place an fragment as overlay. Since Android 5.0 some elements like buttons and cardviews where placed on top. With the solution in the answer, it works like a charm.
-
yk4ever about 7 yearstypo: mMyDrawable.setBounds
-
siriuseteor77 almost 7 yearsThank you so much! You saved my day
-
GhostCat almost 7 yearsI think in order to make this a really helpful answer, you should add a bit more. Example code, screenshots, ...
-
Ranjith Subramaniam over 6 yearsThanks for your solution. But you can access view components from image. For example if there's a button in your view if you click the image, the original view button will be clicked automatically.
-
Jacks Gong over 6 years@RanjithSubramaniam Yes, because of it is just overlay-drawable drawn over the view.
-
Neeraj Sewani over 4 yearsBut it won't work in case a view has to be put on top of another as a drawable is only a graphic
-
user5646514 almost 3 yearsWish I'd have seen this at the beginning of the afternoon. Thanks so much!!