title: Custom TableView with data binding adapter in android
body: <p>I want to achieve this layout. Table can have different number of rows, but once this number is set, it wouldn't change. Also, there is no need for scroll.</p>
  
  <p>Well... I could do this with simple TableLayout, but it doesn't support data binding.</p>
  
  <p>So I'm trying to implement custom AdapterView, but nothing is appeared in a screen.</p>
  
  <pre><code>public class MainActivity extends Activity {
  String[][] data = new String[5][5];
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      RelativeLayout layout = (RelativeLayout) getLayoutInflater().inflate(R.layout.activity_main, null);
      setContentView(layout);
      SquareTableView sqt = new SquareTableView(this);
      sqt.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
      sqt.setNumColumns(5);
      layout.addView(sqt);
      for (int i = 0; i &lt; 5; i++)
          for (int j = 0; j &lt; 5; j++)
              data[i][j] = "a";
      sqt.setAdapter(new CellArrayAdapter(this, data) {
  
      });
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>public class SquareTableView extends AdapterView&lt;ListAdapter&gt; {
  private ListAdapter adapter;
  private int numColumns;
  public int getNumColumns() {
      return numColumns;
  }
  public void setNumColumns(int numColumns) {
      this.numColumns = numColumns;
  }
  
  public SquareTableView(Context context) {
      super(context);
  }
  
  @Override
  public ListAdapter getAdapter() {
      return adapter;
  }
  
  @Override
  public View getSelectedView() {
      // TODO Auto-generated method stub
      return null;
  }
  
  @Override
  public void setAdapter(ListAdapter adapter) {
      this.adapter = adapter;
      requestLayout();
  }
  
  
  
  @Override
  public void setSelection(int position) {
      // TODO Auto-generated method stub
  
  }
  
  
  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      Context context = getContext();
      int orientation = context.getResources().getConfiguration().orientation;
      int sideSize;
      if (orientation ==  Configuration.ORIENTATION_PORTRAIT)
          sideSize = MeasureSpec.getSize(widthMeasureSpec);
      else {
          sideSize = MeasureSpec.getSize(heightMeasureSpec);
      }
      setMeasuredDimension(sideSize, sideSize);
  
  }
  
  @Override
  protected void onLayout(boolean changed, int left, int top, int right,
          int bottom) {
       super.onLayout(changed, left, top, right, bottom);
      for (int i = 0; i &lt; adapter.getCount(); i++) {
          int childSide = getWidth() / numColumns;
          int row = i / numColumns;
          int column = i % numColumns;
          int leftPosition = childSide * column;
          int rightPosition = leftPosition + childSide;
          int topPosition = childSide * row;
          int bottomPosition = topPosition + childSide;
          View child = adapter.getView(i, null, this);
          child.layout(leftPosition, topPosition, rightPosition, bottomPosition);
          child.forceLayout();
      }
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/UskdC.jpg" alt="enter image description here"></p>