Developer
Developer

Reputation: 6350

Layout having two layout in it Horizontally Android

I have this layout. Now i want to use two layout with same details and these two layouts will be inside this layout so that i can show the same layout side by side in a layout Here is my xml file please help me on this :

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:baselineAligned="false" 
    android:gravity="center">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" 
        android:gravity="center">
        <ImageView
            android:id="@+id/flightLogo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/spicejet" />
        <TextView
            android:id="@+id/flightCompany"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textSize="12sp"
            android:text="SpiceJet"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/flightNumber"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textSize="10sp"
            android:text="SG-142" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" 
        android:gravity="center">
        <TextView
            android:id="@+id/departLocation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="BOS"
            android:gravity="center"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/departTime"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="17:40" />

        <TextView
            android:id="@+id/departDate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Jan 19" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" 
        android:gravity="center">

        <TextView
            android:id="@+id/arrivalLocation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="SOF"
            android:gravity="center"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/arrivalTime"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="23:45" />

        <TextView
            android:id="@+id/arrivalDate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Jan 19" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" 
        android:gravity="center">

        <TextView
            android:id="@+id/duration"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="6h45m"
            android:gravity="center"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        android:gravity="center" >

        <TextView
            android:id="@+id/totalCost"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="30,193"
            android:textColor="#0000FF"
            android:gravity="center"
            android:textStyle="bold" />

        <ImageView
            android:id="@+id/bookButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:src="@drawable/book" />
    </LinearLayout>  

</LinearLayout>

Upvotes: 0

Views: 1268

Answers (1)

Cob50nm
Cob50nm

Reputation: 961

both widths for the sub layouts are match_parent so only one will show, match_parent is for the main layout and all sublayouts and elements should have fill_parent instead, Also because you are using weight you can set the width to 0dp and the weight will take care of the scaling

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:baselineAligned="false" 
    android:gravity="center">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" 
        android:gravity="center">
        <ImageView
            android:id="@+id/flightLogo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/spicejet" />
        <TextView
            android:id="@+id/flightCompany"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textSize="12sp"
            android:text="SpiceJet"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/flightNumber"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textSize="10sp"
            android:text="SG-142" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" 
        android:gravity="center">
        <TextView
            android:id="@+id/departLocation"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="BOS"
            android:gravity="center"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/departTime"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="17:40" />

        <TextView
            android:id="@+id/departDate"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Jan 19" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" 
        android:gravity="center">

        <TextView
            android:id="@+id/arrivalLocation"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="SOF"
            android:gravity="center"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/arrivalTime"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="23:45" />

        <TextView
            android:id="@+id/arrivalDate"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Jan 19" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" 
        android:gravity="center">

        <TextView
            android:id="@+id/duration"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="6h45m"
            android:gravity="center"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        android:gravity="center" >

        <TextView
            android:id="@+id/totalCost"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="30,193"
            android:textColor="#0000FF"
            android:gravity="center"
            android:textStyle="bold" />

        <ImageView
            android:id="@+id/bookButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:src="@drawable/book" />
    </LinearLayout>  

</LinearLayout>

Upvotes: 2

Related Questions