Si8
Si8

Reputation: 9225

Change RelativeLayout to add a ListView

I have the following layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
        <EditText
            android:id="@+id/etShowLog"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textMultiLine"
            android:lines="12"
            android:textStyle="normal"
            android:paddingLeft="2dip"
            android:singleLine="false"
            android:textColor="#999999"
            android:textSize="14dip"
            android:gravity="top"
            android:layout_above="@+id/btnClear" />
        <Button
            android:id="@+id/btnClear"
            android:background="@drawable/otherbuttons"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Clear"
            android:textStyle="bold"
            android:textColor="#FFFFFF"
            android:shadowColor="#000000"
            android:shadowDx="1"
            android:shadowDy="1"
            android:shadowRadius="2"
            android:layout_alignParentBottom="true" />
        <Button
            android:id="@+id/btnSave"
            android:background="@drawable/otherbuttons"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Save Trip"
            android:layout_gravity="right"
            android:textStyle="bold"
            android:textColor="#FFFFFF"
            android:shadowColor="#000000"
            android:shadowDx="1"
            android:shadowDy="1"
            android:shadowRadius="2"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true" />
</RelativeLayout>

Which displays this:

enter image description here

How do I edit the layout so I can display a listview underneath:

enter image description here

Upvotes: 0

Views: 243

Answers (3)

BlackLaw
BlackLaw

Reputation: 37

like this,remember to keep listview height in a fixer value or use weight

<?xml version="1.0" encoding="utf-8"?>


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

    <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:layout_marginBottom="200dp">
        <LinearLayout
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="0dp" android:layout_weight="1">
        </LinearLayout>
        <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="New Button"
                    android:id="@+id/button"/>
            <LinearLayout
                    android:layout_width="0px"
                    android:layout_height="wrap_content" android:layout_weight="1">
            </LinearLayout>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="New Button"
                    android:id="@+id/button2"/>
        </LinearLayout>
    </LinearLayout>
    <ListView
            android:layout_width="wrap_content"
            android:layout_height="200dp"
            android:id="@+id/listView" android:layout_gravity="center"
            android:layout_marginTop="-200dp"/>
</LinearLayout>

Upvotes: 1

iaindownie
iaindownie

Reputation: 1056

Here is an alternative (but possibly old, I wrote it a few years ago) layout that includes two side-by-side listviews at the bottom of the view. There are a load of internal views, but you should be able to extract/convert what you need?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent"
android:id="@+id/LinearLayout" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/darkGrey">

<TableLayout android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:id="@+id/TableLayout01"
    android:background="@color/darkGrey">
    <TableRow android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/TableRow01">
        <LinearLayout android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:id="@+id/LinearLayout01">
            <TextView android:id="@+id/strap" android:layout_width="wrap_content"
                android:layout_height="fill_parent" android:textSize="18sp"
                android:text="Pace/Distance/Time\nCalculator" android:textColor="@color/white"
                android:width="170dip" android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true" />
            <Spinner android:layout_height="wrap_content"
                android:layout_width="wrap_content" android:id="@+id/Spinner01"
                android:layout_toRightOf="@+id/strap" android:prompt="@string/Presets" />
        </LinearLayout>
    </TableRow>

    <TableRow android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/TableRow02">
        <LinearLayout android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:id="@+id/LinearLayout02">
            <TextView android:id="@+id/topline1" android:layout_width="wrap_content"
                android:layout_below="@+id/strap" android:layout_height="wrap_content"
                android:text=" Hour" android:width="80dip" />
            <TextView android:id="@+id/topline2" android:layout_width="wrap_content"
                android:layout_below="@+id/strap" android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/topline1" android:text="Mins"
                android:width="80dip" />
            <TextView android:id="@+id/topline3" android:layout_width="wrap_content"
                android:layout_below="@+id/strap" android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/topline2" android:text="Secs" />
        </LinearLayout>
    </TableRow>

    <TableRow android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/TableRow03">
        <LinearLayout android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:id="@+id/LinearLayout03">
            <EditText android:text="a" android:id="@+id/EditText01a"
                android:layout_below="@+id/strap" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:layout_weight="1"
                android:maxLength="2" android:inputType="number" />
            <EditText android:text="b" android:id="@+id/EditText01b"
                android:layout_width="fill_parent" android:layout_height="wrap_content"
                android:layout_weight="1" android:maxLength="2" android:inputType="number" />
            <EditText android:text="c" android:id="@+id/EditText01c"
                android:layout_width="fill_parent" android:layout_height="wrap_content"
                android:layout_weight="1" android:maxLength="6" android:inputType="numberDecimal" />
            <Button android:layout_width="fill_parent" android:text="Time"
                android:layout_height="wrap_content" android:layout_weight="1"
                android:onClick="myClickHandler" android:id="@+id/Button01" />
        </LinearLayout>
    </TableRow>

    <TableRow android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/TableRow04">
        <LinearLayout android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:id="@+id/LinearLayout04">
            <TextView android:id="@+id/midline1" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:text=" Enter Distance (miles)" />
        </LinearLayout>
    </TableRow>
    <TableRow android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/TableRow05">
        <LinearLayout android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:id="@+id/LinearLayout05">
            <EditText android:layout_height="wrap_content"
                android:text="@+id/EditText02" android:layout_width="wrap_content"
                android:id="@+id/EditText02" android:inputType="numberDecimal"
                android:maxLength="8" android:width="120dip" />
            <Button android:id="@+id/Button02" android:layout_height="wrap_content"
                android:layout_width="wrap_content" android:layout_toRightOf="@+id/EditText02"
                android:layout_below="@+id/midline1" android:text="Distance"
                android:layout_marginLeft="103dip" android:width="100dip"
                android:maxWidth="100dip" android:onClick="myClickHandler" />
        </LinearLayout>
    </TableRow>

    <TableRow android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/TableRow06">
        <LinearLayout android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:id="@+id/LinearLayout06">
            <TextView android:id="@+id/topline1" android:layout_width="wrap_content"
                android:layout_below="@+id/strap" android:layout_height="wrap_content"
                android:text=" Hour" android:width="80dip" />
            <TextView android:id="@+id/topline2" android:layout_width="wrap_content"
                android:layout_below="@+id/strap" android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/topline1" android:text="Mins"
                android:width="80dip" />
            <TextView android:id="@+id/topline3" android:layout_width="wrap_content"
                android:layout_below="@+id/strap" android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/topline2" android:text="Secs" />
        </LinearLayout>
    </TableRow>

    <TableRow android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/TableRow07">
        <LinearLayout android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:id="@+id/LinearLayout07">
            <EditText android:text="a2" android:id="@+id/EditText03a"
                android:layout_below="@+id/strap" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:layout_weight="1"
                android:maxLength="2" android:inputType="number" />
            <EditText android:text="b2" android:id="@+id/EditText03b"
                android:layout_width="fill_parent" android:layout_height="wrap_content"
                android:layout_weight="1" android:maxLength="2" android:inputType="number" />
            <EditText android:text="c2" android:id="@+id/EditText03c"
                android:layout_width="fill_parent" android:layout_height="wrap_content"
                android:layout_weight="1" android:maxLength="6" android:inputType="numberDecimal" />
            <Button android:layout_width="fill_parent" android:text="Pace"
                android:layout_height="wrap_content" android:layout_weight="1"
                android:onClick="myClickHandler" android:id="@+id/Button03" />
        </LinearLayout>
    </TableRow>

    <TableRow android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/TableRow08"
        android:background="@color/darkGrey">
        <LinearLayout android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:id="@+id/LinearLayout08"
            android:background="@color/darkGrey">
            <ListView android:id="@+id/ListView01"
                android:layout_weight="1" android:layout_height="wrap_content"
                android:layout_width="fill_parent" android:background="@color/darkGrey" />
            <ListView android:id="@+id/ListView02"
                android:layout_weight="1" android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:layout_toRightOf="@+id/ListView01"
                android:background="@color/darkGrey2" />
        </LinearLayout>
    </TableRow>


</TableLayout>

Upvotes: 1

codeMagic
codeMagic

Reputation: 44571

Wrap your Buttons in a LinearLayout. Give the ListView

android:"@+id/myListView"
android:layout_alignParentBottom="true"

and the newly created LinearLayout

android:layout_above="@id/myListView

So something like this should get you close

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
    <EditText
        android:id="@+id/etShowLog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textMultiLine"
        android:lines="12"
        android:textStyle="normal"
        android:paddingLeft="2dip"
        android:singleLine="false"
        android:textColor="#999999"
        android:textSize="14dip"
        android:gravity="top"
        android:layout_above="@+id/btnClear" />
<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_above="@id/myListView">
    <Button
        android:id="@+id/btnClear"
        android:background="@drawable/otherbuttons"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Clear"
        android:textStyle="bold"
        android:textColor="#FFFFFF"
        android:shadowColor="#000000"
        android:shadowDx="1"
        android:shadowDy="1"
        android:shadowRadius="2"
        android:layout_gravity="left"/>
    <Button
        android:id="@+id/btnSave"
        android:background="@drawable/otherbuttons"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save Trip"
        android:layout_gravity="right"
        android:textStyle="bold"
        android:textColor="#FFFFFF"
        android:shadowColor="#000000"
        android:shadowDx="1"
        android:shadowDy="1"
        android:shadowRadius="2"
        android:layout_gravity="left" />
</LinearLayout>
<ListView
  android:id="@+id/myListView"
  ...
  android:layout_alignParentBottom="true"
</RelativeLayout>

I also took out the RelativeLayout properties that were in the Buttons and added android:layout_graivty="left" android:layout_gravity="right"

Upvotes: 1

Related Questions