Android – Dynamic TableLayout not appearing-Collection of common programming errors
I am trying to implement a HUD display to my cutsom keyboard. The HUD will contain pairs of key/value. I need to programatically create a TableLayout and draw it on my canvas. In order to do this, I am overriding the keyboardview onDraw() method:
@Override
public void onDraw(Canvas canvas) { // In the custom MyKeyboardView class
MyHUD hud = new MyHUD(getContext());
super.onDraw(canvas);
hud.tl.draw(canvas); //tl being the TableLayout to draw
}
MyHUD class:
public class MyHUD {
public TableLayout tl;
private MyData mData;
public MyHUD(Context context) {
TableRow.LayoutParams trParams = new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
TableLayout.LayoutParams tlParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
tl = new TableLayout(context);
tl.setLayoutParams(tlParams);
for (int i = 0; i < 10; i++) {
TableRow tr = new TableRow(context);
tr.setId(100+i);
tr.setLayoutParams(trParams);
TextView charTV = new TextView(context);
charTV.setId(200+i);
charTV.setText("Hello " + i);
charTV.setTextColor(Color.RED);
//charTV.setLayoutParams(trParams);
tr.addView(charTV);
TextView keyTV = new TextView(context);
keyTV.setId(300+i);
keyTV.setText("world " + i);
keyTV.setTextColor(Color.CYAN);
//keyTV.setLayoutParams(trParams);
tr.addView(keyTV);
tl.addView(tr, tlParams);
}
}
}
(Note that currently there is no data fetching, just a “Hello world” for test purposes). I have commented out the TextView layouts, as seen on several other posts, but that doesn’t seem to be the problem.
Alternative option:
Instead of instanciating the TableLayout via a constructor, I have tried something like:
tl = (TableLayout) findViewById(R.id.my_hud);
Along with the following XML:
The problem is that the findViewById method is undefined for my custom class.
Any idea why it is not properly drawing the TableLayout?