Question

I have programmatically created a TableLayout. Everything is working good. Except the background-colors and text-colors. I wanted the background-color of my TableLayout to be black and the text-color of each TextView should be white. Now I've set the two colors in my colors.xml (#fffff and #000000) and set the BackgroundResource for my table black and the TextColor for all my TextViews white. But the only thing showing is a black screen without any white text in it. I can't see any mistake in my code. Maybe you can ;-)

public class ShowTableActivity extends Activity {

TableLayout t1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    t1 = createTable();
    fillTable();

    setContentView(t1);


}

public TableLayout createTable() {
    /** create table */
    TableLayout table = new TableLayout(this);
    table.setStretchAllColumns(true);
    table.setBackgroundResource(R.color.black);
    table.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT));

    /** title column/row */
    TableRow rowTitle = new TableRow(this);
    rowTitle.setGravity(Gravity.CENTER_HORIZONTAL);

    TextView title = new TextView(this);
    title.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    title.setGravity(Gravity.CENTER_HORIZONTAL);
    title.setText("Liganame");
    title.setTextSize(20);
    title.setTextColor(R.color.white);
    TableRow.LayoutParams params = new TableRow.LayoutParams();
    params.span = 9;
    rowTitle.addView(title, params);

    /** header row */
    TableRow rowHeader = new TableRow(this);
    rowHeader.setGravity(Gravity.CENTER_HORIZONTAL);
    TableRow.LayoutParams params1 = new TableRow.LayoutParams();
    params1.setMargins(5, 5, 5, 5);

    TextView rangHeader = new TextView(this);
    rangHeader.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));

    TextView mannschaftsnameHeader = new TextView(this);
    mannschaftsnameHeader.setLayoutParams(new LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    TextView spieleHeader = new TextView(this);
    spieleHeader.setLayoutParams(new LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    spieleHeader.setGravity(Gravity.CENTER_HORIZONTAL);
    spieleHeader.setText("Sp");
    spieleHeader.setTextSize(16);
    spieleHeader.setTextColor(R.color.white);

    TextView siegeHeader = new TextView(this);
    siegeHeader.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    siegeHeader.setGravity(Gravity.CENTER_HORIZONTAL);
    siegeHeader.setText("S");
    siegeHeader.setTextSize(16);
    siegeHeader.setTextColor(R.color.white);

    TextView unentschiedenHeader = new TextView(this);
    unentschiedenHeader.setLayoutParams(new LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    unentschiedenHeader.setGravity(Gravity.CENTER_HORIZONTAL);
    unentschiedenHeader.setText("U");
    unentschiedenHeader.setTextSize(16);
    unentschiedenHeader.setTextColor(R.color.white);

    TextView niederlagenHeader = new TextView(this);
    niederlagenHeader.setLayoutParams(new LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    niederlagenHeader.setGravity(Gravity.CENTER_HORIZONTAL);
    niederlagenHeader.setText("N");
    niederlagenHeader.setTextSize(16);
    niederlagenHeader.setTextColor(R.color.white);

    TextView toreHeader = new TextView(this);
    toreHeader.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    toreHeader.setGravity(Gravity.CENTER_HORIZONTAL);
    toreHeader.setText("Tore");
    toreHeader.setTextSize(16);
    toreHeader.setTextColor(R.color.white);
    TableRow.LayoutParams params2 = new TableRow.LayoutParams();
    params2.span = 3;

    TextView differenzHeader = new TextView(this);
    differenzHeader.setLayoutParams(new LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    differenzHeader.setGravity(Gravity.CENTER_HORIZONTAL);
    differenzHeader.setText("Diff");
    differenzHeader.setTextSize(16);
    differenzHeader.setTextColor(R.color.white);

    TextView punkteHeader = new TextView(this);
    punkteHeader.setLayoutParams(new LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    punkteHeader.setGravity(Gravity.CENTER_HORIZONTAL);
    punkteHeader.setText("Pkt");
    punkteHeader.setTextSize(16);
    punkteHeader.setTextColor(R.color.white);

    rowHeader.addView(rangHeader, new TableRow.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    rowHeader.addView(mannschaftsnameHeader, new TableRow.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    rowHeader.addView(spieleHeader, new TableRow.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    rowHeader.addView(siegeHeader, new TableRow.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    rowHeader.addView(unentschiedenHeader, new TableRow.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    rowHeader.addView(niederlagenHeader, new TableRow.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    rowHeader.addView(toreHeader, params2);
    rowHeader.addView(differenzHeader, new TableRow.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    rowHeader.addView(punkteHeader, new TableRow.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    table.addView(rowTitle);
    table.addView(rowHeader, params1);

    return table;
}

public void fillTable() {

    TableRow tr = new TableRow(this);

    TextView rang = new TextView(this);
    TextView mannschaftsname = new TextView(this);
    TextView spiele = new TextView(this);
    TextView siege = new TextView(this);
    TextView unentschieden = new TextView(this);
    TextView niederlagen = new TextView(this);
    TextView tore = new TextView(this);
    TextView doppelpunkt = new TextView(this);
    TextView gegentore = new TextView(this);
    TextView differenz = new TextView(this);
    TextView punkte = new TextView(this);

    rang.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    rang.setGravity(Gravity.CENTER_HORIZONTAL);
    rang.setText("0");
    rang.setTextColor(R.color.white);

    mannschaftsname.setLayoutParams(new LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    mannschaftsname.setGravity(Gravity.CENTER_HORIZONTAL);
    mannschaftsname.setText("BBBBBBBBBB");
    mannschaftsname.setTextColor(R.color.white);

    spiele.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    spiele.setGravity(Gravity.CENTER_HORIZONTAL);
    spiele.setText("0");
    spiele.setTextColor(R.color.white);

    siege.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    siege.setGravity(Gravity.CENTER_HORIZONTAL);
    siege.setText("0");
    siege.setTextColor(R.color.white);

    unentschieden.setLayoutParams(new LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    unentschieden.setGravity(Gravity.CENTER_HORIZONTAL);
    unentschieden.setText("0");
    unentschieden.setTextColor(R.color.white);

    niederlagen.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    niederlagen.setGravity(Gravity.CENTER_HORIZONTAL);
    niederlagen.setText("0");
    niederlagen.setTextColor(R.color.white);

    tore.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    tore.setGravity(Gravity.CENTER_HORIZONTAL);
    tore.setText("0");
    tore.setTextColor(R.color.white);

    doppelpunkt.setLayoutParams(new TableRow.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    doppelpunkt.setGravity(Gravity.CENTER_HORIZONTAL);
    doppelpunkt.setText(":");
    doppelpunkt.setTextColor(R.color.white);

    gegentore.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    gegentore.setGravity(Gravity.CENTER_HORIZONTAL);
    gegentore.setText("0");
    gegentore.setTextColor(R.color.white);

    differenz.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    differenz.setGravity(Gravity.CENTER_HORIZONTAL);
    differenz.setText("0");
    differenz.setTextColor(R.color.white);

    punkte.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    punkte.setGravity(Gravity.CENTER_HORIZONTAL);
    punkte.setText("0");
    punkte.setTextColor(R.color.white);

    tr.addView(rang, new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    tr.addView(mannschaftsname, new TableRow.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    tr.addView(spiele, new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    tr.addView(siege, new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    tr.addView(unentschieden, new TableRow.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    tr.addView(niederlagen, new TableRow.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    tr.addView(tore, new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    tr.addView(doppelpunkt, new TableRow.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    tr.addView(gegentore, new TableRow.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    tr.addView(differenz, new TableRow.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    tr.addView(punkte, new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));

    t1.addView(tr, new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));
  }
}
Was it helpful?

Solution

Solved my problem after massive research:

instead of using R.color.white and R.color.black, you have to put in 0xff000000 (black) and 0xffffffff (white). After the first 2 ff the color code starts, so you can pick any you want.

Don't ask me why you have to do it like this. Maybe somebody else can explain it here at some time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top