Michael Meyer
Michael Meyer

Reputation: 2184

Placing Fragment below Listview

I am struggling with my Android Layout.

I want to create a Linear Layout which contains a Button at the Top, next a List View and when all elements of the list view are displayed I want to show a Fragment which displays Google Maps.

This is my Layout xml file. In that case you can only see the Fragment and the Button but not the listview

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:paddingLeft="10dp"
              android:paddingRight="10dp"
              android:orientation="vertical"
        tools:context="de.mypackage.MyActivity">

    <Button
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:layout_weight="0"
            android:id="@+id/button_back"
            android:text="@string/back"
            />

        <ListView
                android:id="@+id/list"
                android:layout_height="fill_parent"
                android:layout_width="fill_parent"
                android:layout_weight="1"/>
                />

    <fragment
            android:id="@+id/fragement"
            android:name="de.mpackage.MapsFragment"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:layout_weight="0"
            tools:layout="@layout/fragment_maps"/>

</LinearLayout>

Any Idea how I can realize my layout?

Upvotes: 1

Views: 161

Answers (5)

natsumiyu
natsumiyu

Reputation: 3257

I found a solution hope it helps. In my ListView for the height instead of using 0dp I used wrap_content and adds layout_weight=1 for my Fragment

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#EFEFEF"
    android:orientation="vertical">

     <Button
        android:id="@+id/button_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        />

     <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:divider="@null"/>

    <fragment  
        android:name="de.mpackage.MapsFragment"
        android:id="@+id/fragement"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_margin="5dp"
        android:layout_weight="1"
        />

</LinearLayout>

Upvotes: 0

Hiren Gondaliya
Hiren Gondaliya

Reputation: 156

Try this :

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:tools="http://schemas.android.com/tools"
                  android:layout_width="wrap_content"
                  android:layout_height="fill_parent"
                  android:paddingLeft="10dp"
                  android:paddingRight="10dp"
                  tools:context="de.mypackage.MyActivity">

             <Button
                android:layout_height="wrap_content"
                android:layout_width="fill_parent"
                android:layout_weight="0"
                android:id="@+id/button_back"
                android:text="@string/back"
                />

            <ListView
                    android:id="@+id/list"
                    android:layout_height="fill_parent"
                    android:layout_width="fill_parent"
                    android:layout_weight="1"
                    android:layout_below="@+id/button_back"/>
                    />

        <fragment
                android:id="@+id/fragement"
                android:name="de.mpackage.MapsFragment"
                android:layout_height="wrap_content"
                android:layout_width="fill_parent"
                android:layout_weight="0"
                android:layout_below="@+id/list"
                tools:layout="@layout/fragment_maps"/>

    </RelativeLayout>

Upvotes: 0

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

Advice : Declare your root layout as RelativeLayout

RelativeLayout is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements .

Then use android:layout_below

Positions the top edge of this view below the given anchor view ID. Accommodates top margin of this view and bottom margin of anchor view.

<fragment
        android:id="@+id/fragement"
        android:name="de.mpackage.MapsFragment"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_below="@+id/birth"
        tools:layout="@layout/list"/>

Upvotes: 1

user3040153
user3040153

Reputation: 169

Try this code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingLeft="10dp"
    android:weightSum="1"
    android:paddingRight="10dp">

    <Button
        android:id="@+id/button_back"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/back" />

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5" />

    <fragment
        android:id="@+id/fragement"
        android:name="com.yougotag.supplierfieldagent.fragments.AboutFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        tools:layout="@layout/fragment_about_app" />

</LinearLayout>

this will set both in half screen you can change ration of weights in layout.

Upvotes: 1

Pratik Popat
Pratik Popat

Reputation: 2999

Try this code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingLeft="10dp"
    android:paddingRight="10dp">

    <Button
        android:id="@+id/button_back"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/back" />

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/fragement"
        android:name="com.yougotag.supplierfieldagent.fragments.AboutFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:layout="@layout/fragment_about_app" />

</LinearLayout>

Upvotes: 0

Related Questions