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 of self.view
. Adding it to self.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?
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.
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). Interaction with the overlay layer is done by adding and removing drawables.
RelativeLayout is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout area (such as aligned to the bottom, left or center).
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)
).
<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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With