javaandroidtablelayout

Have a problem on creating table Android Studio


I am trying to make a simple app. The program should create a table with given row and column numbers. I'm adding my Java and xml files below:

public class MainActivity extends AppCompatActivity {

    private Button btnCreate, btnCalculate, btnReset, btnExit;
    private EditText txtColumn, txtRow;
    private LinearLayout linLayout, mainLayout;
    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();

        LinearLayout mainLayout = findViewById(R.id.mainLayout);
        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(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);
                }

                mainLayout.addView(table);

            }
        });


        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);
            }
        });
    }
}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <EditText
        android:id="@+id/txtRow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter row number"
        android:inputType="textCapWords"
        android:textSize="18sp" />

    <EditText
        android:id="@+id/txtColumn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter column number"
        android:inputType="textCapWords"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/txtResult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Result"
        android:background="#E91E63"
        android:textSize="18sp"
        />

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btnCreate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="CREATE"/>

        <Button
            android:id="@+id/btnCalculate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="CALCULATE" />

        <Button
            android:id="@+id/btnReset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RESET" />
        <Button
            android:id="@+id/btnExit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="EXIT" />
    </LinearLayout>

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


    </LinearLayout>

</LinearLayout>

This is the error message that I got:

The specified child already has a parent. You must call removeView() on the child's parent first.

Therefore, whenever I tried to add mainLayout.removeView(table); to the code nothing happens when I click the button create. Thank you all. I will be appreciated for any kinds of comments.


Solution

  • You have the following errors:

    1: The layout above the mainLayout layout is letting the height be match_parent. Make mainLayout not displayable.

    2: The layout mainLayout is also setting the height to match_parent which is also wrong. I wonder do you understand what match_parent means?

    3: The first time you add successfully, but because you are setting the height to match_parent, it makes it not visible, in fact it successfully added the table. The second click because it has been added to the table, it will crash.

    The code below I have fixed for you, let's try it.

    XML:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
    
        <EditText
            android:id="@+id/txtRow"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter row number"
            android:inputType="textCapWords"
            android:textSize="18sp"
            android:text="3"/>
    
        <EditText
            android:id="@+id/txtColumn"
            android:text="2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter column number"
            android:inputType="textCapWords"
            android:textSize="18sp" />
    
        <TextView
            android:id="@+id/txtResult"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Result"
            android:background="#E91E63"
            android:textSize="18sp"
            />
    
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <Button
                android:id="@+id/btnCreate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="CREATE"/>
    
            <Button
                android:id="@+id/btnCalculate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="CALCULATE" />
    
            <Button
                android:id="@+id/btnReset"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="RESET" />
            <Button
                android:id="@+id/btnExit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="EXIT" />
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/mainLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
    
        </LinearLayout>
    
    </LinearLayout>
    

    JAVA:

    public class MainAct extends AppCompatActivity {
    
        private Button btnCreate, btnCalculate, btnReset, btnExit;
        private EditText txtColumn, txtRow;
        private LinearLayout linLayout, mainLayout;
        private TextView txtResult;
        private Random random;
        TableLayout table;
    
        @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();
    
            LinearLayout mainLayout = findViewById(R.id.mainLayout);
    
    
            btnCalculate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                }
            });
    
            btnCreate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (table != null) {
                        mainLayout.removeView(table);
                    }
                    table = new TableLayout(getBaseContext());
    
                    table.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                    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(MainAct.this);
                        for (int j=0; j < columnNumber; j++) {
                            int value = random.nextInt(100) + 1;
                            TextView tv = new TextView(MainAct.this);
                            tv.setText(String.valueOf(value));
                            row.addView(tv);
                        }
                        table.addView(row);
                    }
                    if (table != null) {
                        mainLayout.addView(table);
                    }
                }
            });
    
    
            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);
                }
            });
        }
    }