Add an array of buttons to a GridView in an Android application

In the following code, you should change the upper limits of the for to a variable.

public class MainActivity
        extends Activity
        implements View.OnClickListener {

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

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

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

        for (int f=0; f<=13; f++) {
            TableRow tr = new TableRow(this);
            for (int c=0; c<=9; c++) {
                Button b = new Button (this);
                b.setText(""+f+c);
                b.setTextSize(10.0f);
                b.setTextColor(Color.rgb( 100, 200, 200));
                b.setOnClickListener(this);
                tr.addView(b, 30,30);
            } // for
            layout.addView(tr);
        } // for

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

    public void onClick(View view) {
        ((Button) view).setText("*");
        ((Button) view).setEnabled(false);
    }
} // class

Leave a Comment