My table is populated by dynamic data, so I am adding TableRows and its Views in the code. The TableLayout and the first row (my header) are built in the xml. Gravity set to center works on these items. But setting the Gravity to center on my dynamically created TableRows and Views is not working.
<TableLayout
android:id="@+id/poweredEquip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center"
android:padding="10dip">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/tab_bg_unselected"
android:gravity="center">
<TextView
android:text="Serial"
android:textSize="15sp"
android:textColor="@android:color/black"
android:layout_width="130px"
android:layout_height="wrap_content"
android:ellipsize="middle"
android:gravity="center"
android:layout_column="1"/>
<TextView
android:text="Model"
android:textSize="15sp"
android:textColor="@android:color/black"
android:layout_width="110px"
android:layout_height="wrap_content"
android:ellipsize="middle"
android:gravity="center"
android:layout_column="2"/>
<TextView
android:text="Status"
android:textSize="15sp"
android:textColor="@android:color/black"
android:layout_width="150px"
android:layout_height="wrap_content"
android:ellipsize="middle"
android:gravity="center"
android:layout_column="3"/>
<TextView
android:text="Tool Turned"
android:textSize="15sp"
android:textColor="@android:color/black"
android:layout_width="100px"
android:layout_height="wrap_content"
android:ellipsize="middle"
android:gravity="center"
android:layout_column="4"/>
<TextView
android:text="Hours"
android:textSize="15sp"
android:textColor="@android:color/black"
android:layout_width="110px"
android:layout_height="wrap_content"
android:ellipsize="middle"
android:gravity="center"
android:layout_column="5"/>
<TextView
android:text="Trailer Model"
android:textSize="15sp"
android:textColor="@android:color/black"
android:layout_width="110px"
android:layout_height="wrap_content"
android:ellipsize="middle"
android:gravity="center"
android:layout_column="6"/>
</TableRow>
<TableLayout>
Activity.cs
TableLayout powered = (TableLayout) FindViewById(Resource.Id.poweredEquip);
TableRow.LayoutParams p = new TableRow.LayoutParams(ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.WrapContent) {Gravity = GravityFlags.Center};
TableRow.LayoutParams p1 = new TableRow.LayoutParams(ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.WrapContent) {Column = 1, Gravity = GravityFlags.Center};
TableRow row = new TableRow(this) { LayoutParameters = p };
text = new TextView(this) {Text = dr["Serial"].ToString(), LayoutParameters = p1};
text.SetPadding(8, 8, 8, 8);
text.SetMaxWidth(150);
text.SetMinWidth(150);
row.AddView(text);
powered.AddView(row);
Can you post your XML layout for the textview’s (just 1 or 2) that work?
Stab in the dark, but you might want to try setting the LAYOUT_GRAVITY for the text’s layout parameters instead of the GRAVITY since you are forcing the width to be 150. Specifying the Layout Gravity as CENTER should force the entire TEXTVIEW to move to the middle of the parent container (Table Row).
Another thought: When you set the layout params for the TextView, it’s using TableRow.LayoutParams. I think it might actually be setting the parameters in reference to the table row, not setting the parameters of the TextView itself. Try calling this on each TextView you create before adding it to the table row:
text.setGravity(Gravity.CENTER);