1

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?

Saim Mahmood
  • 426
  • 2
  • 12

2 Answers2

2

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>
Darish
  • 11,032
  • 5
  • 50
  • 70
  • can you please explain further? – Saim Mahmood Mar 16 '17 at 18:50
  • The second layout is inflated at run time into the first layout. Read my answer once again. best wishes :) – Darish Mar 16 '17 at 18:53
  • no i was asking that if i just edit the text and image views and addView in this, would it work? – Saim Mahmood Mar 16 '17 at 18:54
  • wait... i will post the xml too – Darish Mar 16 '17 at 18:56
  • can you help me out with the java part as well? – Saim Mahmood Mar 16 '17 at 19:07
  • 1
    first_layout is part of the mainActivity and you can access it via findViewById. Then the second_layout.xml is inflated using the above code and added. If you want 10 rows, inflate the same layout 10 times and add it into the container layout. cheers (Y) – Darish Mar 16 '17 at 19:10
  • ok, but didnt you just split the layout into 2 separate layout files? what if i dont want to do that ... ? – Saim Mahmood Mar 16 '17 at 19:13
  • ok. if you don't split the layouts, it will get appended as it is. – Darish Mar 16 '17 at 19:21
  • but when I try to use this line : View inflatedLayout= inflater.inflate(R.layout.second_layout, null, false); it gives me an error on the R.layout.second_layout line, what to do there? I also gave my second linearlayout an id of linear_layout_five – Saim Mahmood Mar 16 '17 at 19:22
  • second_linear layout is not an id, it is the name of the xml file which you are trying to inflate – Darish Mar 16 '17 at 19:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/138264/discussion-between-saim-mahmood-and-darish). – Saim Mahmood Mar 16 '17 at 19:28
1

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 :)

Ali Nasir
  • 217
  • 2
  • 12