I am making a simple soundboard app – buttons should appear in two columns, pressing a button plays a sound. I made it and had all the buttons defined using XML and it worked fine, but since the app was already attaching a listener to play a sound using a loop, I decided to define the buttons using only code.
So the code has no errors and the app launches but no buttons appear. There is a similar problem here: Content not showing in dynamically created android TableLayout although if I understand it correctly, the proposed solution (ensure Im using TableLayout.LayoutParams) is already applied here.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main_table2);
int[] sounds = { ... };
String[] labels = { ... };
declareButtons(sounds, labels);
}
private void declareButtons(int[] sounds, String[] labels)
{
TableLayout tbl = (TableLayout) this.findViewById(R.id.tblMain);
TableRow row = null;
Button cell;
for (int i=0; i<sounds.length; i++)
{
cell = new Button(this);
cell.setText(labels[i]);
cell.setOnClickListener(this.getStartListener(sounds[i]));
cell.setBackgroundDrawable(getResources().getDrawable(R.drawable.soundbutton));
cell.setTextColor(color.white);
TableLayout.LayoutParams cellParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT, (float) 1);
cellParams.leftMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics());
cellParams.topMargin = cellParams.leftMargin;
if (i%2==0) // left column
{
row = new TableRow(this);
cell.setLayoutParams(cellParams);
row.addView(cell);
}
else // right column
{
cellParams.rightMargin = cellParams.leftMargin;
cell.setLayoutParams(cellParams);
row.addView(cell);
row.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
tbl.addView(row);
}
}
if (sounds.length%2 > 0) // handle uneven amount of buttons
tbl.addView(row);
}
And here is the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/leonbg2"
android:orientation="vertical" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableLayout
android:id="@+id/tblMain"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TableLayout>
</ScrollView>
</LinearLayout>
Here is how table rows were defined previously, which worked fine:
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/buttonaw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:background="@drawable/soundbutton"
android:text="A what?"
android:textColor="@android:color/white" />
<Button
android:id="@+id/buttona"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:background="@drawable/soundbutton"
android:text="Argh!"
android:textColor="@android:color/white" />
</TableRow>
Well, I managed to fix it myself by getting rid of
TableLayoutand going for a bunch ofLinearLayouts (one for each row). The code is more or less the same with the addition ofrow.setOrientation(LinearLayout.HORIZONTAL);beforecell.setLayoutParams(cellParams);in the left column part of the if statement. The moral of the story is; if you think you need to use TableLayout, you probably dont. Peace.