Is it possible to float elements above other elements in Android so that they do NOT affect the layout of their container?
The effect I want to achieve is a floating speech-bubble-like element:
The yellow box above is the element I want to float above the other elements, but as you can see it pushes its container down creating the black space below the green bar, instead of floating above the gray area below it.
RelativeLayout can position the element appropriately but I cannot find a way to make the positioned element not expand the boundary of its container. Is this even possible on Android?
ANALOGIES
The float CSS property places an element on the left or right side of its container, allowing text and inline elements to wrap around it. The element is removed from the normal flow of the page, though still remaining a part of the flow (in contrast to absolute positioning).
The floated element floats on top of the other elements by default, sticking to the right or left of its parent body, depending on the value we set. Only text and inline level elements will not overlap the floating element but will wrap around it.
The CSS float property controls the positioning and formatting of content on the page. Its most common use is to wrap text around images. However, you can use the float property to wrap any inline elements around a defined HTML element, including lists, paragraphs, divs, spans, tables, iframes, and blockquotes.
Take a look at the Relative layouts in android: for example when you dont specify a position at all of the views within the relative layout they will float on top of each other.
http://developer.android.com/guide/topics/ui/layout/relative.html
Example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#444444"
android:text="Update"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/lost_of_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/some_button"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="text view with lots of text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/some_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Some Button" />
</RelativeLayout>
Edit: CommonsWare was faster ;) take a look at his beautiful answer!
RelativeLayout can position the element appropriately but I cannot find a way to make the positioned element not expand the boundary of its container.
Then you have the wrong container.
For example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/big"
android:textSize="120dip"
android:textStyle="bold"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/small"/>
</RelativeLayout>
Here, the small button floats over top of the large button. The small button does not "expand the boundary of its container", simply because the container is the entire content view.
You set up the RelativeLayout
(or FrameLayout
) container to be the one that contains the possible area your child can float over. Whether that is the entire screen or merely a subset is up to you. Conversely, if your child is affecting the boundary of its container, you are using the wrong container, or need to set up a container specifically for this role.
In another example, Roman Nurik shows how to implement a floating "undo bar", akin to Gmail's:
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