5

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?

1
  • I think the table.setShrinkAllColumns(true); table.setStretchAllColumns(true); is just messing up the tables what I see, without them the basic TableRows are fine. Can anyone explain why these should be added?? Commented Oct 6, 2024 at 17:03

1 Answer 1

10

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);
Sign up to request clarification or add additional context in comments.

4 Comments

The code you provided worked, but when I run the application, nothing happened. I gave the parameters but table is not created.
it's because you not add table view in any view, you just create a table but not add that table to any view of xml
Thanks for your reply! I created a new LinearLayout inside my XML and gave a unique id to it. Then I added my table inside this layout. Now, I have to work on my table styling. Thank you for your answer!
@rawsly happy to help you..!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.