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.
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):
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);
}
});
}
}
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.