Saim Mahmood
Saim Mahmood

Reputation: 446

add LinearLayout within another LinearLayout through java

I have a layout that acts as a type of table which has two linearlayouts. The main linearlayout has vertical orientation and the one inside this one has a horizontal orientation. The code is below ...

XML file ...

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/view_below_linear_layout_three_team_spuck"
    android:background="@drawable/cell_shape"
    android:id="@+id/linear_layout_four_team_spuck"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="45dp">

        <TextView
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:text="1"
            android:id="@+id/serial_number_team_spuck"
            android:gravity="center"
            android:background="@drawable/cell_shape"
            android:textSize="20dp"
            />

        <ImageView
            android:layout_width="50dp"
            android:layout_height="35dp"
            android:layout_gravity="center"
            android:id="@+id/the_smallperson_2_team_spuck"
            android:src="@drawable/the_smallperson"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/serial_number_team_spuck"
            android:layout_toEndOf="@+id/serial_number_team_spuck" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textColor="#B2B2B2"
            android:textSize="12dp"
            android:gravity="center"
            android:id="@+id/spuck_table_name"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/the_smallperson_2_team_spuck"
            android:layout_toEndOf="@+id/the_smallperson_2_team_spuck"
            android:layout_marginLeft="14dp"
            android:layout_marginStart="14dp" />



        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textColor="#B2B2B2"
            android:textSize="12dp"
            android:gravity="center"
            android:id="@+id/spuck_table_speed"
            android:layout_marginLeft="30dp"
            android:layout_marginStart="30dp"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/spuck_table_name"
            android:layout_toEndOf="@+id/spuck_table_name" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textColor="#B2B2B2"
            android:textSize="12dp"
            android:gravity="center"
            android:id="@+id/spuck_table_points"
            android:layout_marginLeft="29dp"
            android:layout_marginStart="39dp"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/spuck_table_speed"
            android:layout_toEndOf="@+id/spuck_table_speed" />

    </LinearLayout>
</LinearLayout>

I want to add new entries into this layout and view this as a table. The entries I want to add are the three textviews inside the second linearlayout. Since I am getting the data through php database I want to achive this through java, any idea on how to do this?

Upvotes: 1

Views: 540

Answers (2)

Ali Nasir
Ali Nasir

Reputation: 217

Change your xml saim, you should be using a simple TableLayout for these kind of things.

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/view_below_linear_layout_three_team_spuck"
    android:id="@+id/the_maintable_team_spuck"
    >

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/the_maintable_row_team_spuck">

    </TableRow>
</TableLayout>

I didnt add any items in the row because 'n' numbers of items can be added through java. Their font, background, gravity and etc everything. Hope this helps :)

Upvotes: 1

Darish
Darish

Reputation: 11481

add LinearLayout within another LinearLayout through java

You can inflate the layout like this

  LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
 View inflatedLayout= inflater.inflate(R.layout.second_layout, null, false);
 firstLayout.addView(inflatedLayout);

first_layout.xml

   <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/view_below_linear_layout_three_team_spuck"
    android:background="@drawable/cell_shape"
    android:id="@+id/linear_layout_four_team_spuck"
    android:orientation="vertical">

</LinearLayout>

second_layout.xml

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="45dp">

    <TextView
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:text="1"
        android:id="@+id/serial_number_team_spuck"
        android:gravity="center"
        android:background="@drawable/cell_shape"
        android:textSize="20dp"
        />

    <ImageView
        android:layout_width="50dp"
        android:layout_height="35dp"
        android:layout_gravity="center"
        android:id="@+id/the_smallperson_2_team_spuck"
        android:src="@drawable/the_smallperson"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/serial_number_team_spuck"
        android:layout_toEndOf="@+id/serial_number_team_spuck" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textColor="#B2B2B2"
        android:textSize="12dp"
        android:gravity="center"
        android:id="@+id/spuck_table_name"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/the_smallperson_2_team_spuck"
        android:layout_toEndOf="@+id/the_smallperson_2_team_spuck"
        android:layout_marginLeft="14dp"
        android:layout_marginStart="14dp" />



    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textColor="#B2B2B2"
        android:textSize="12dp"
        android:gravity="center"
        android:id="@+id/spuck_table_speed"
        android:layout_marginLeft="30dp"
        android:layout_marginStart="30dp"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/spuck_table_name"
        android:layout_toEndOf="@+id/spuck_table_name" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textColor="#B2B2B2"
        android:textSize="12dp"
        android:gravity="center"
        android:id="@+id/spuck_table_points"
        android:layout_marginLeft="29dp"
        android:layout_marginStart="39dp"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/spuck_table_speed"
        android:layout_toEndOf="@+id/spuck_table_speed" />

</LinearLayout>

Upvotes: 2

Related Questions