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?

2
  • If you want to make a table like view then RecyclerView or ListView will be a better idea instead of this. Commented Mar 16, 2017 at 19:17
  • actually this isnt my code, i am just doing this for a friend. He likes it this way .... Commented Mar 16, 2017 at 19:21

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

11 Comments

can you please explain further?
The second layout is inflated at run time into the first layout. Read my answer once again. best wishes :)
no i was asking that if i just edit the text and image views and addView in this, would it work?
wait... i will post the xml too
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)
|
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 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.