Search code examples
androidxmluser-interfaceandroid-linearlayoutandroid-relativelayout

Android - LinearLayout/RelativeLayout Alignment


I am attempting to create a simple UI that looks something like thisenter image description here

I am very new to android and have been trying different types of layouts (Linear/Relative) and I believe Linear may be the way to go here (I hope to make the amount of ListViews dynamic at some stage but for now 2 is fine).

Should I be using a Linear Layout or Relative layout, or some combination of both to achieve this.

Here is the XML which I have, I can't seem to get the button to align right, even though its gravity is set to right. I am open to any suggestions on how to fix this, or if there is a better way

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.9"

    android:orientation="horizontal" >

    <ListView
        android:id="@+id/lv1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        />

    <ListView
        android:id="@+id/lv2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.1"
    android:orientation="horizontal" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="New Button"
        android:id="@+id/button"
        android:gravity="right" />
</LinearLayout>

edit : here is what the UI currently looks likeenter image description here


Solution

  • <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <LinearLayout
            android:id="@+id/ll"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentTop="true"
            android:layout_marginTop="18dp"
            android:layout_above="@+id/btnButton"
            android:weightSum="2"
            android:orientation="horizontal" >
    
            <ListView 
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                android:layout_weight="1">
    
            </ListView>
            <ListView 
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                android:layout_weight="1">
    
            </ListView>
        </LinearLayout>
    
        <Button 
            android:id="@+id/btnButton"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="Button"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"/>
    
    </RelativeLayout>