Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dividing a RelativeLayout into two halves

Hi I have created a RelativeLayout containing 2 buttons, a radio button and a graph view.

I want to display two different data in graph now.

How do I split the RelativeLayout into two halves?

This is my present XML code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Button
        android:id="@+id/BtnStart"
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="5dip"
        android:text="SlaveEnable" />

    <Button
        android:id="@+id/BtnStop"
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="5dip"
        android:layout_toRightOf="@id/BtnStart"
        android:text="SlaveDisable" />

    <RadioButton
        android:id="@+id/BtnSaveFile"
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@id/BtnStop"
        android:text="SaveFile" />

    <helog.diwesh.NugaBest.GraphView
        android:id="@+id/gview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/BtnStart"
        android:layout_alignParentRight="true" />

</RelativeLayout>
like image 312
Rahul Sharma Avatar asked Aug 26 '13 03:08

Rahul Sharma


2 Answers

So, for this, the best way I've found (which feels like a total hack, but works like a charm) is to have a zero-size View aligned to the center of the layout, then align the left half to the left of that View, the align the right half to the right of that View. For 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" >

    <View
        android:id="@+id/anchor"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerInParent="true" />

    <Button
        android:id="@+id/left_side"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/anchor"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="5dip"
        android:text="Left Side" />

    <Button
        android:id="@+id/right_side"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/anchor"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="5dip"
        android:text="Right Side" />

</RelativeLayout>
like image 158
Kevin Coppock Avatar answered Nov 23 '22 23:11

Kevin Coppock


Try this layout. This should work with some changes if not perfect. This will add two graphs.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Button
        android:id="@+id/BtnStart"
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="5dip"
        android:text="SlaveEnable" />

    <Button
        android:id="@+id/BtnStop"
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="5dip"
        android:layout_toRightOf="@id/BtnStart"
        android:text="SlaveDisable" />

    <RadioButton
        android:id="@+id/BtnSaveFile"
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@id/BtnStop"
        android:text="SaveFile" />

<LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
            android:layout_above="@id/BtnStart"
            android:layout_alignParentRight="true"
        android:orientation="vertical" 
    android:weightSum="2" >

    <helog.diwesh.NugaBest.GraphView
                android:id="@+id/gview1"
                android:layout_height="0dp"
                android:layout_width="match_parent"
        android:layout_weight="1" />

    <helog.diwesh.NugaBest.GraphView
                android:id="@+id/gview2"
                android:layout_height="0dp"
                android:layout_width="match_parent"
        android:layout_weight="1" />

</LinearLayout>

</RelativeLayout>
like image 37
Sagar Avatar answered Nov 23 '22 21:11

Sagar