Developer
Developer

Reputation: 6350

Layout Weight in Linear Layout Android?

I am using two ListView in my Layout. and having fixed header and footer.But when i am running my code my header is getting breaked not coming full on my Device and my footer is not showing because of the ListView .

This is my main XML file that is having the ListView

<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:id="@+id/llHeader"
        android:orientation="vertical"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_weight="1">
        <LinearLayout
            android:id="@+id/mainLinearLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:background="@drawable/top_bg"
            android:orientation="horizontal" >
        <ImageView 
            android:id="@+id/back_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/back_btn"
            android:layout_marginTop="5dp"
            android:layout_marginLeft="10dp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="75dp"
            android:text="Flights"
            android:textColor="@android:color/white" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/flightDetailsLayout"
        android:layout_width="fill_parent"
        android:layout_height="70dp"
        android:layout_below="@+id/mainLinearLayout"
        android:layout_centerHorizontal="true"
        android:background="@drawable/gray_bg123"
        android:orientation="vertical" >
        <LinearLayout
            android:id="@+id/innerflightDetailsLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/from"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp"
                android:paddingLeft="12dp"
                android:textSize="12sp"
                android:text="Boston to San Fransisco, "
                android:textStyle="bold" />

            <TextView
                android:id="@+id/tripType"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp"
                android:text="RoundTrip"
                android:textSize="12sp" />

        </LinearLayout>
        <LinearLayout
            android:id="@+id/innerTripLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/tripDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="12dp"
                android:textSize="10sp"
                android:text="19 Jan 2013 " 
                android:textStyle="bold"/>
            <TextView
                android:id="@+id/paxText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="for 1 Adults,0 Childs,O Infants"
                android:textSize="12dp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/Book"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginTop="5dp"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/tripDate"
                android:layout_width="84dp"
                android:layout_height="wrap_content"
                android:paddingLeft="12dp"
                android:text="Rs 10,2345 "
                android:textSize="10sp"
                android:layout_weight="1"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/bookBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="left"
                android:src="@drawable/book" />

        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/sortFlightLayouts"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/flightDetailsLayout"
        android:background="@drawable/gray_bg123"
        android:orientation="horizontal" >

    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="DEL-BLR"
                android:layout_marginLeft="6dp"
                android:layout_marginTop="5dp"
                android:textStyle="bold" />

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"

                android:orientation="horizontal" >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:src="@drawable/flight_icon_result" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:layout_marginLeft="25dp"
                    android:src="@drawable/time" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:layout_marginLeft="5dp"
                    android:src="@drawable/rupee" />
            </LinearLayout>
        </LinearLayout>
        <View
            android:layout_width="1dp"
            android:layout_height="60dp"
            android:background="@android:color/darker_gray" />
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/gray_bg123"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="BLR-DEL"
                android:layout_marginLeft="6dp"
                android:layout_marginTop="5dp"
                android:textStyle="bold" />

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:src="@drawable/flight_icon_result" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:layout_marginLeft="25dp"
                    android:src="@drawable/time" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:layout_marginLeft="5dp"
                    android:src="@drawable/rupee" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
        </LinearLayout>

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_weight="4"
        android:orientation="horizontal">

        <ListView
            android:id="@+id/lvDepartures"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>
        <ListView
             android:id="@+id/lvArrivals"
             android:layout_weight="1"
             android:layout_height="wrap_content"
             android:layout_width="wrap_content"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/llFooter"
        android:orientation="vertical"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_weight="1"/>
        <LinearLayout
        android:id="@+id/footerLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:background="@drawable/top_bg" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_marginTop="5dp"
            android:src="@drawable/filter" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_marginTop="5dp"
            android:src="@drawable/sort" />
    </LinearLayout>
</LinearLayout> 

And this my Layout that i am putting in the ListView at RunTime

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

    <ImageView
        android:id="@+id/flightLogo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="5sp"
        android:layout_marginRight="10sp"
        android:layout_marginTop="5sp"
        android:src="@drawable/spicejet" />

    <TextView
        android:id="@+id/flightCompanyName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/flightLogo"
        android:text="Spice Jet" />

    <TextView
        android:id="@+id/flightNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/flightCompanyName"
        android:text="9W-123" />

    <TextView
        android:id="@+id/flightTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/flightLogo"
        android:gravity="center_vertical"
        android:text="6:00 - 7:00" />

    <TextView
        android:id="@+id/flightStop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/flightTime"
        android:layout_toRightOf="@+id/flightLogo"
        android:text="1h 35m | Non Stop" />

    <TextView
        android:id="@+id/amount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/flightStop"
        android:layout_toRightOf="@+id/flightLogo"
        android:text="Rs 20,000" />

</RelativeLayout>

Upvotes: 0

Views: 1732

Answers (1)

codeMagic
codeMagic

Reputation: 44571

I can't tell if this is your only problem yet but when you use layout_weight inside of a vertical LinearLayout then your layout_height should be 0dp.

Likewise if it is inside of a horizontal LinearLayout then the layout_width should be 0dp. Also, all of the layouts should have a weight if you are using it on one of thenm.

android:layout_below= in your third LinearLayout, I believe, is not a property of LinearLayout. That is a property of RelativeLayout.

My suggestion for this type of setup is to use RelativeLayout as the root layout. Then, for your header LinearLayout you can use android:layout_alignParentTop="true" and for your footer use android:layout_alignParentBottom="true" then stick your ListView in the middle with android:layout_below="@id/idOfHeaderLL"

<RelativeLayout
  ...>
    <LinearLayout
      android:id="@+id/headerLL"
      android:layout_alignParentTop="true"
      ...>
     <!-- add header veiws -->
    </LinearLayout
    <ListView
       android:layout_below="@id/headerLL"
       .../>
    <LinearLayout
       android:layout_alignParentBottom="true"
       ...>
    <!-- add footer views -->
    </LinearLayout>
</RelativeLayout>

I'm not going to rewrite the whole layout but that should give you an idea. The "..." is where you will put in your other properties such as height, width, etc...

Upvotes: 2

Related Questions