out_sid3r
out_sid3r

Reputation: 1028

TextView layout_gravity not working programmatically

I've been looking around and have found many possible solutions for centering a textview in a layout. But none worked for me. My textview is in a TableLayout which is described by the following xml:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/schedule_main_holder">
    <TableLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:stretchColumns="0"
      android:id="@+id/schedule_table_entry">
   </TableLayout>
</LinearLayout>

What I'm doing is making a new TableRow and then adding a TextView and a ListView to it...but the textview has to be centered vertically. For that I'm doing:

TableRow row = new TableRow(this);

        TextView tview = new TextView(this);
        tview.setText("Wednesday");
        TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_VERTICAL);
        tview.setLayoutParams(layoutParams);
        row.addView(tview);

The problem is that the TextView is always on top of the cell and not in the middle as it should. I've tried a mix of combinations (even the FrameLayout method describe in another response) and I can't get the textview to be centered in the table cell.

Thanks in advance

Upvotes: 0

Views: 3530

Answers (1)

Warpzit
Warpzit

Reputation: 28152

First there are two types of gravity.I think your using regular gravity not the layout one. second you could also create the view in xml then use inflatrer and then add. This gives much cleaner code aswel.

Edit: So your too lazy to ACTUALLY try layout inflater and listen to advice, here is the code:

main.xml

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="0"
android:id="@+id/schedule_table_entry">
</TableLayout>

myrow.xml

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
    android:id="@+id/text"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="center" />
</TableRow>

TestActivity

public class TestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    LayoutInflater inflater = getLayoutInflater();
    TableRow row = (TableRow) inflater.inflate(R.layout.myrow, null);
    TextView text = (TextView) row.findViewById(R.id.text);
    text.setText("I did all your work... smart even");

    TableLayout table = (TableLayout) findViewById(R.id.schedule_table_entry);
    table.addView(row);
}

Upvotes: 1

Related Questions