2

I found this example

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TableLayout layout = new TableLayout (this);
        layout.setLayoutParams( new TableLayout.LayoutParams(20,20) );

        layout.setPadding(1,1,1,1);



        for (int f=0; f<=3; f++) {
            TableRow tr = new TableRow(this);
            tr.setPadding(10,10,10,10);

            for (int c=0; c<=3; c++) {


                Button b = new Button (this);
                b.setText(""+f+c);
                b.setTextSize(10.0f);
                b.setPadding(10, 10, 10, 10);

                b.setTextColor(Color.rgb( 100, 200, 200));
                b.setBackgroundColor(Color.BLUE);
                b.setOnClickListener(this);
                b.setWidth(24);
                b.setHeight(24);
                tr.addView(b);
            } // for
            layout.addView(tr);
        } // for

        super.setContentView(layout);
    } // ()

I need to have matrix of buttons ( something like GridLayout in Java ). The problem in this code is that I don't have any space between columns in same row. How to add space between buttons in same row ?

Damir
  • 54,277
  • 94
  • 246
  • 365

1 Answers1

4

I think you need to set margins for your buttons, because padding may only size down the top layer of the button and not its background. Here is an example how to do something similar in code: Programmatically set margin for TableRow

However in this example you need to change the parent container to TableRow, because layout parameters always refer to its immediate parent, which for your buttons is TableRow.

Community
  • 1
  • 1
Lumis
  • 21,517
  • 8
  • 63
  • 67