1

When I create the rows in the Android Studio Design tab, the button within the rows don't seem to be properly aligned when I created them via XML. Studio Designer

However, when I programmatically add them via inflation, they get aligned properly (note that the last row with Key Name was copied and pasted directly into the XML, so I can test if there is something wrong with my table settings):

enter image description here

Here is the xml file for the row:

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/active_key_row"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="5pt"
    android:layout_marginEnd="5pt"
    android:visibility="visible">

    <TextView
        android:id="@+id/active_key_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Key Name" />

    <Button
        android:id="@+id/active_key_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/remove_key" />
</TableRow>

Here is the XML for the table:

<TableLayout
    android:id="@+id/bt_keys_table"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TableRow xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/active_key_row"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="5pt"
        android:layout_marginEnd="5pt"
        android:visibility="visible">

        <TextView
            android:id="@+id/active_key_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Key Name" />

        <Button
            android:id="@+id/active_key_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/remove_key" />
    </TableRow>

    <TableRow
        android:id="@+id/empty_key_row"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="5pt"
        android:layout_marginEnd="5pt"
        android:visibility="visible">


        <TextView
            android:id="@+id/empty_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="" />

        <Button
            android:id="@+id/add_key_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:enabled="false"
            android:text="@string/add_key" />
    </TableRow>

</TableLayout>

Here is the Java code for the inflation:

private void buildBtKeysTable() {

    mBtKeysTable = (TableLayout) findViewById(R.id.bt_keys_table);

    // Add the rows to the table
    for (int i = 0; i < DEFAULT_MAX_KEYS; i++) {
        // Inflate the row
        final TableRow row = (TableRow)
                getLayoutInflater().inflate(R.layout.active_keys_table_row, null);

        mBtKeysTable.addView(row, 0);

        Button button = (Button) row.findViewById(R.id.active_key_button);

        // Subscribe to the active buttons for bluetooth
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                removeBluetoothKey(row);
            }
        });
    }
}
Mark Adler
  • 101,978
  • 13
  • 118
  • 158
Kiril
  • 39,672
  • 31
  • 167
  • 226
  • remove android:layout_marginEnd="5pt" and check it again. – Radhey Oct 27 '16 at 07:55
  • @Redhey that worked, but I have the margin set on the TableRow xml file as well, and it doesn't seem to be a problem when created programmatically. That's really strange :/ – Kiril Oct 27 '16 at 07:59
  • 1
    That is because you are passing in null as the parent ViewGroup parameter to inflate(). This will cause all layout_* attributes to be ignored, as the inflater has no idea which attributes are valid for the container in which it will be placed (i.e. it has no idea which LayoutParams type to set on the View). – Radhey Oct 27 '16 at 08:04
  • 1
    try to replace this getLayoutInflater().inflate(R.layout.active_keys_table_row, null); with View child = getLayoutInflater().inflate(R.layout.active_keys_table_row, item, false); and figure out what is happening. – Radhey Oct 27 '16 at 08:06
  • your WC any time and every time :) – Radhey Oct 27 '16 at 08:55
  • @Radhey Please add your comment as an answer so I can mark it as accepted. – Kiril Oct 27 '16 at 13:09

2 Answers2

1

Sir ,that is because you are passing null as the parent ViewGroup parameter to inflate(). This will cause all layout_* attributes to be ignored, as the inflater has no idea which attributes are valid for the container in which it will be placed (i.e. it has no idea which LayoutParams type to set on the View).

let we explore more ..

try to replace this

getLayoutInflater().inflate(R.layout.active_keys_table_row, null);
                            with         
getLayoutInflater().inflate(R.layout.active_keys_table_row, item, false);

and figure out what is happening.

Radhey
  • 2,139
  • 2
  • 24
  • 42
0

Add weight attribute to button also. if not remove weight from both textview and button and try to add gravity to right for the button.

Ishan Fernando
  • 2,758
  • 1
  • 31
  • 38