Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Float views so they do not take up space in the layout

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:

enter image description here

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

  • in the web world the equivalent is an element which has 'position: absolute' and is thus not counted as part of the "flow" (ie: layout)
  • in the WPF world the equivalent would be an element which is positioned inside a Canvas. The Canvas has its own bounds which take up space in the layout but its children can lie outside of its bounds and not affect the layout
  • in the iOS world every view is positioned as a canvas (since there are no layout managers) so this behavior is achieved bysimply offsetting the element frame's x and y properties
like image 295
Marchy Avatar asked Sep 25 '12 22:09

Marchy


People also ask

What is a float layout?

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).

How do floated elements behave?

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.

When to use CSS float?

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.


2 Answers

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!

like image 65
Rolf ツ Avatar answered Oct 24 '22 22:10

Rolf ツ


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>

Floating buttons

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:

Floating Undo Bar

like image 41
CommonsWare Avatar answered Oct 25 '22 00:10

CommonsWare