Simple Table for Android

This demo illustrates a custom Android table constructed from a grid of text views. It is for display only; dynamically resizing columns or selecting items is not supported.

import android.app.Activity;
import android.content.Context;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.R;
import android.view.Gravity;

color BLUE = color(64, 124, 188);
color LTGRAY = color(185, 180, 180);
color YELLOW = color(245, 250, 13);
color RED = color(255, 0, 0);
color BLACK = color(0, 0, 0);
color WHITE = color(255, 255, 255);
color GREEN = color(32, 175, 47);
color ORANGE = color(237, 147, 29);

Activity activity;
Context ctx;
FrameLayout layout;

int _numRows = 4;
int _numCols = 3;

void table(int left, int top, int w, int h, int colGap, int rowGap) {

  String str[] = {"ITEM", "COLOR", "FOR SALE", "Boat", "Yellow", "Yes", "Car", "Magenta", "Yes", "House", "White", "No"};

  int id = 0;
  for (int k = 0; k < _numRows; k++) {
    for (int j = 0; j < _numCols; j++) {
      int x = left + j*(w+colGap);
      int y = top + k*(h+rowGap);
      strokeWeight(4.0);
      if (k == 0) {
        textView(x, y, w, h, str[id], LTGRAY, WHITE, 26.0);
      } else {
        textView(x, y, w, h, str[id], WHITE, BLACK, 22.0);
      }
      id++;
    }
  }
}

void setup() {
  fullScreen();
  orientation(LANDSCAPE);
  background(BLUE);
  activity = this.getActivity();
  ctx = activity.getApplicationContext();
  runOnUiThread( new Runnable() {
    void run() {
      table(300, 300, 320, 80, 0, 0);
    }
  }
  );
}

void draw() {
}

TextView (separate Tab):

void textView(int x, int y, int w, int h, String txtStr, color bkgrndColor, color txtColor, float txtSize) {

  TextView textView = new TextView(ctx);
  textView.setLayoutParams (new RelativeLayout.LayoutParams (RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
  textView.setX(x);
  textView.setY(y);
  textView.setWidth(w);
  textView.setHeight(h);
  textView.setText(txtStr);
  textView.setBackgroundColor(bkgrndColor);
  textView.setTextColor(txtColor);
  textView.setTextSize(txtSize);
  textView.setGravity(Gravity.CENTER);
  FrameLayout flayout = (FrameLayout)activity.findViewById(R.id.content);
  flayout.addView(textView);
}

1 Like