3

Is there a way to implement a working reusable border layout in android ? One that behaves just like swing's BorderLayout: Maximizing the middle and reducing the rest to minimal size ?

3 Answers 3

6

Try out this and you will get the same behaviour as Swings BorderLayout (the result is shown in the picture):

BorderLayout Demo

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<TextView
    android:id="@+id/north"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="@android:color/holo_orange_light"
    android:gravity="center_horizontal"
    android:text="North"
    android:textAppearance="@android:style/TextAppearance.Large" />

<TextView
    android:id="@+id/south"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@android:color/holo_blue_light"
    android:gravity="center_horizontal"
    android:text="South"
    android:textAppearance="@android:style/TextAppearance.Large" />

<TextView
    android:id="@+id/west"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_above="@id/south"
    android:layout_alignParentLeft="true"
    android:layout_below="@id/north"
    android:background="@android:color/holo_red_light"
    android:gravity="center_vertical"
    android:text="West"
    android:textAppearance="@android:style/TextAppearance.Large" />

<TextView
    android:id="@+id/east"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_above="@id/south"
    android:layout_alignParentRight="true"
    android:layout_below="@id/north"
    android:background="@android:color/holo_purple"
    android:gravity="center_vertical"
    android:text="East"
    android:textAppearance="@android:style/TextAppearance.Large" />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@id/south"
    android:layout_below="@id/north"
    android:layout_toLeftOf="@id/east"
    android:layout_toRightOf="@id/west"
    android:background="@android:color/holo_green_light"
    android:gravity="center"
    android:text="Center"
    android:textAppearance="@android:style/TextAppearance.Large" />

1
  • When you want to replace a view programmaticaly, get the id and the layout parameters of the textviews above
    – hfmanson
    May 9, 2019 at 18:51
2

You can use a RelativeLayout together with android:layout_weight attributes to get the same effect.

2
  • R U sure there is an attribute "layout_weight" ? (Android 2.1 API Level 7)
    – Bigbohne
    Nov 4, 2011 at 10:48
  • @Bigbohne - From the docs for R.attr: "public static final int layout_weight Since: API Level 1". Note that the namespace qualifier android: is essential. There is no attribute layout_weight, but there is one android:layout_weight.
    – Ted Hopp
    Nov 4, 2011 at 14:12
1

I did not want to use XML layout files and I find the use of RelativeLayout in code cumbersome. Therefore, I implemented my own BorderLayout class:

public class BorderLayout extends ViewGroup {

    private View left;
    private View top;
    private View right;
    private View bottom;
    private View center;

    public BorderLayout(Context context) {
        super(context);
    }

    public void setLeft(View left) {
        if (this.left != null)
            removeView(this.left);
        this.left = left;
        if (this.left != null)
            addView(this.left);
    }

    public void setTop(View top) {
        if (this.top != null)
            removeView(this.top);
        this.top = top;
        if (this.top != null)
            addView(this.top);
    }

    public void setRight(View right) {
        if (this.right != null)
            removeView(this.right);
        this.right = right;
        if (this.right != null)
            addView(this.right);
    }

    public void setBottom(View bottom) {
        if (this.bottom != null)
            removeView(this.bottom);
        this.bottom = bottom;
        if (this.bottom != null)
            addView(this.bottom);
    }

    public void setCenter(View center) {
        if (this.center != null)
            removeView(this.center);
        this.center = center;
        if (this.center != null)
            addView(this.center);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int topWidth = 0;
        int topHeight = 0;
        if (top != null) {
            top.measure(widthMeasureSpec, MeasureSpec.UNSPECIFIED);
            topWidth = top.getMeasuredWidth();
            topHeight = top.getMeasuredHeight();
        }
        int bottomWidth = 0;
        int bottomHeight = 0;
        if (bottom != null) {
            bottom.measure(widthMeasureSpec, MeasureSpec.UNSPECIFIED);
            bottomWidth = bottom.getMeasuredWidth();
            bottomHeight = bottom.getMeasuredHeight();
        }
        int remainingHeightSpec = GuiUtil.subtractFromMeasureSpec(heightMeasureSpec, topHeight + bottomHeight);
        int leftWidth = 0;
        int leftHeight = 0;
        if (left != null) {
            left.measure(MeasureSpec.UNSPECIFIED, remainingHeightSpec);
            leftWidth = left.getMeasuredWidth();
            leftHeight = left.getMeasuredHeight();
        }
        int rightWidth = 0;
        int rightHeight = 0;
        if (right != null) {
            right.measure(MeasureSpec.UNSPECIFIED, remainingHeightSpec);
            rightWidth = right.getMeasuredWidth();
            rightHeight = right.getMeasuredHeight();
        }
        int remainingWidthSpec = GuiUtil.subtractFromMeasureSpec(widthMeasureSpec, leftWidth + rightWidth);
        int centerWidth = 0;
        int centerHeight = 0;
        if (center != null) {
            center.measure(remainingWidthSpec, remainingHeightSpec);
            centerWidth = center.getMeasuredWidth();
            centerHeight = center.getMeasuredHeight();
        }
        int width = Util.max(topWidth, leftWidth + centerWidth + rightWidth, bottomWidth);
        int height = topHeight + Util.max(leftHeight, centerHeight, rightHeight) + bottomHeight;
        setMeasuredDimension(GuiUtil.getDefaultSize(width, widthMeasureSpec), GuiUtil.getDefaultSize(height, heightMeasureSpec));
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int w = r - l;
        int h = b - t;
        int topHeight = 0;
        if (top != null) {
            topHeight = top.getMeasuredHeight();
            top.layout(0, 0, w, topHeight);
        }
        int bottomHeight = 0;
        if (bottom != null) {
            bottomHeight = bottom.getMeasuredHeight();
            bottom.layout(0, h - bottomHeight, w, h);
        }
        int leftWidth = 0;
        if (left != null) {
            leftWidth = left.getMeasuredWidth();
            left.layout(0, topHeight, leftWidth, h - bottomHeight);
        }
        int rightWidth = 0;
        if (right != null) {
            rightWidth = right.getMeasuredWidth();
            right.layout(w - rightWidth, topHeight, w, h - bottomHeight);
        }
        if (center != null)
            center.layout(leftWidth, topHeight, w - rightWidth, h - bottomHeight);
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.