0

I have a TableLayout defined as a layout resource which I am trying to programmatically add to a vertical LinearLayout whose gravity is "center". This is the code I use:

LayoutInflater inflater = getLayoutInflater();
LinearLayout ll = (LinearLayout) findViewById(R.id.testLinearLayout);
TableLayout myTable = (TableLayout) inflater.inflate(R.layout.table2x2, null);
ll.addView(myTable);

myTable gets added but instead of being in the center, it's vertically centered and aligned to the left. If instead of adding it programmatically I hardcode the same TableLayout definition inside the LinearLayout the center gravity is respected. I know I must be missing something, but I can't figure it out.

LinearLayout:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:id="@+id/testLinearLayout"
    android:layout_weight="1"
    android:gravity="center">
</LinearLayout>

It has a weight because it's part of an horizontal layout.

TableLayout:

<?xml version="1.0" encoding="utf-8"?>

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

<TableRow
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:text="\"
        android:id="@+id/textView2x2_o"
        android:gravity="center" />

    <TextView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:text="0"
        android:id="@+id/textView2x2_c0"
        android:gravity="center" />

    <TextView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:text="1"
        android:id="@+id/textView2x2_c1"
        android:gravity="center" />

</TableRow>

<TableRow
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:text="0"
        android:id="@+id/textView2x2_r0"
        android:gravity="center" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="0"
        android:id="@+id/textView2x2_0"
        android:gravity="center" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="0"
        android:id="@+id/textView2x2_1"
        android:gravity="center" />

</TableRow>

<TableRow
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:text="1"
        android:id="@+id/textView2x2_c2"
        android:gravity="center" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="0"
        android:id="@+id/textView2x2_4"
        android:gravity="center" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="0"
        android:id="@+id/textView2x2_5"
        android:gravity="center" />

</TableRow>

</TableLayout>

1 Answers1

0

You're not passing the root to the inflate method

TableLayout myTable = (TableLayout) inflater.inflate(R.layout.table2x2, null);

Should be

TableLayout myTable = (TableLayout) inflater.inflate(R.layout.table2x2, ll, false);

More info here: http://www.doubleencore.com/2013/05/layout-inflation-as-intended/

Pedro Oliveira
  • 20,442
  • 8
  • 55
  • 82
  • It works! I had tried to specify the LinearLayout as the root with the two arguments method but it made my application crash. Thank you specially for that link with the explanation! – cthulhufhtagn Oct 07 '14 at 10:50
  • Probably you passed true as the 3rd parameter. The 3rd parameter tells if you should or not attach the view to the root. If you set it to true and then you try to add the view you will get an exception telling you that the view already has a parent. This means that, if you pass true you should remove the addView, if you pass false you should call addView. – Pedro Oliveira Oct 07 '14 at 10:52