Android Creating Table Programmatically

rawsly picture rawsly · Nov 24, 2017 · Viewed 7.8k times · Source

I am trying to generate a number table with given row and column number. The program takes two parameters from the user and creating a table rows and columns with given parameters. Each cell is going to have a random number value between 1 to 100. As far as, I coded my XML with 2 different EditText views which are going to be my parameters. There are four buttons "Create", "Calculate", "Reset", "Exit" that I am going to use. When the user clicks "Create" button, table will be generated with the given row and column values. Therefore I have to create my table from my java file instead of XML file.

This is my code so far:

public class MainActivity extends AppCompatActivity {

private Button btnCreate, btnCalculate, btnReset, btnExit;
private EditText txtColumn, txtRow;
private LinearLayout linLayout;
private TableLayout table;
private TextView txtResult;
private Random random;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnCreate = (Button) findViewById(R.id.btnCreate);
    btnCalculate = (Button) findViewById(R.id.btnCalculate);
    btnReset = (Button) findViewById(R.id.btnReset);
    btnExit = (Button) findViewById(R.id.btnExit);

    txtColumn = (EditText) findViewById(R.id.txtColumn);
    txtRow = (EditText) findViewById(R.id.txtRow);
    txtResult = (TextView) findViewById(R.id.txtResult);
    txtColumn.requestFocus();

    random = new Random();

    TableLayout table = new TableLayout(this);

    btnCalculate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });

    btnCreate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            table.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            table.setShrinkAllColumns(true);
            table.setStretchAllColumns(true);
            int columnNumber = Integer.parseInt(txtColumn.getText().toString());
            int rowNumber = Integer.parseInt(txtRow.getText().toString());

            for(int i=0; i<rowNumber; i++) {
                TableRow row = new TableRow();
                for(int j=0; i<columnNumber; i++) {
                    int value = random.nextInt(100) + 1;
                    TextView tv = new TextView();
                    tv.setText(String.valueOf(value));
                    row.addView(tv);
                }
                table.addView(row);
            }
        }
    });

    btnExit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            System.exit(0);
        }
    });
    btnReset.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            txtColumn.setText(null);
            txtRow.setText(null);
            txtResult.setText(null);
        }
    });
}
}

I am getting an error at my "for" loops. It says "TableRow" has to get Context parameter inside. However, I really didn't understand what kind of parameter it wants. I tried to search for table creation programmatically, however no luck so far. How am I supposed to fix this error?

Answer

Munir picture Munir · Nov 24, 2017

You need to pass context while creating a table row. try below code

for (int i=0; i < rowNumber; i++) {
  TableRow row = new TableRow(MainActivity.this);
      for (int j=0; j < columnNumber; j++) {
        int value = random.nextInt(100) + 1;
        TextView tv = new TextView(MainActivity.this);
        tv.setText(String.valueOf(value));
        row.addView(tv);
      }
  table.addView(row);
}

And you also need to add tablelayout to any view of xml for example in your xml file add liner layout like below

<LinearLayout 
    android:id="@+id/mainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"/>

Cast that view in java file like below

LinearLayout mainLayout = findViewById(R.id.mainLayout); 

Add your table layout to this view like below

mainLayout.addView(table);