Wednesday, 2 October 2019

Application doing too much work in main thread loading the xml layout file

I am making a 9x9 Sudoku grid where each of the 81 cells is itself a 3x3 grid. A single cell looks something like this:

1 2 3

4 5 6

7 8 9

Each number represents pencil annotations for that cell. I have a file called cell_layout.xml representing this 3x3 arrangement.

I am already able to generate the grid, and the code works:

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

        TableLayout sudokuGrid = (TableLayout) findViewById(R.id.sudokuGrid);
        sudokuGrid.setShrinkAllColumns(true);
        sudokuGrid.setStretchAllColumns(true);

        TableRow.LayoutParams paramsRow = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
        TableLayout.LayoutParams paramsLayout = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);

        for(int i = 0; i < 9; ++i)
        {
            TableRow tableRow = new TableRow(SolverActivity.this);
            tableRow.setDividerDrawable(getResources().getDrawable(R.drawable.column_divider));
            tableRow.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
            for(int j = 0; j < 9; ++j)
            {
                View cell = getLayoutInflater().inflate(R.layout.cell_layout, sudokuGrid, false);
                cell.setLayoutParams(paramsRow);
                tableRow.addView(cell);
            }

            tableRow.setLayoutParams(paramsLayout);
            sudokuGrid.addView(tableRow);
        }

    }

The code above just inflates 81 times the wanted layout into the TableLayout.

It works, so what is your problem?

The activity takes too long to create. Even if I test with just one row of the grid, it takes too long for the method to inflate many times the wanted layout.

I get:

Background concurrent copying GC freed 131244(9MB) AllocSpace objects, 0(0B) LOS objects, 24% free, 74MB/98MB, paused 127us total 444.411ms

Skipped 153 frames! The application may be doing too much work on its main thread.

Can anyone suggest a better approach for my situation? Is really too much work generating 81 times a 3x3 grid?

Thanks

Edit

So I now tried to manually write down the xml file. I thought that not having to inflate many times the xml would improve things, but it didn´t. I still have the same problem "Too much work in the main thread" just by loading the xml file.

<TableLayout
        android:id="@+id/sudokuGrid"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:divider="@drawable/row_divider"
        android:showDividers="middle"
        android:shrinkColumns="*"
        android:stretchColumns="*"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/solverTitle">

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:divider="@drawable/column_divider"
            android:showDividers="middle">
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:divider="@drawable/column_divider"
            android:showDividers="middle">
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:divider="@drawable/column_divider"
            android:showDividers="middle">
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:divider="@drawable/column_divider"
            android:showDividers="middle">
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:divider="@drawable/column_divider"
            android:showDividers="middle">
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:divider="@drawable/column_divider"
            android:showDividers="middle">
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:divider="@drawable/column_divider"
            android:showDividers="middle">
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:divider="@drawable/column_divider"
            android:showDividers="middle">
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:divider="@drawable/column_divider"
            android:showDividers="middle">
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <include
                layout="@layout/cell_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
        </TableRow>

    </TableLayout>

And here is my cell layout, in case someone wants to give it a try.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/cellValue"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintDimensionRatio="1:1"
        android:gravity="center"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/pencilOne"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintDimensionRatio="1:1"
        android:text="1"
        android:textSize="10sp"
        android:gravity="center"
        android:visibility="invisible"
        app:layout_constraintBottom_toTopOf="@+id/pencilFour"
        app:layout_constraintEnd_toStartOf="@+id/pencilTwo"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/pencilTwo"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintDimensionRatio="1:1"
        android:text="2"
        android:textSize="10sp"
        android:gravity="center"
        android:visibility="invisible"
        app:layout_constraintBottom_toTopOf="@+id/pencilFive"
        app:layout_constraintEnd_toStartOf="@+id/pencilThree"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/pencilOne"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/pencilThree"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintDimensionRatio="1:1"
        android:text="3"
        android:textSize="10sp"
        android:gravity="center"
        android:visibility="invisible"
        app:layout_constraintBottom_toTopOf="@+id/pencilSix"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/pencilTwo"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/pencilFour"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintDimensionRatio="1:1"
        android:text="4"
        android:textSize="10sp"
        android:gravity="center"
        android:visibility="invisible"
        app:layout_constraintBottom_toTopOf="@+id/pencilSeven"
        app:layout_constraintEnd_toStartOf="@+id/pencilFive"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/pencilOne" />

    <TextView
        android:id="@+id/pencilSix"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintDimensionRatio="1:1"
        android:text="6"
        android:textSize="10sp"
        android:gravity="center"
        android:visibility="invisible"
        app:layout_constraintBottom_toTopOf="@+id/pencilNine"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/pencilFive"
        app:layout_constraintTop_toBottomOf="@+id/pencilThree" />

    <TextView
        android:id="@+id/pencilSeven"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintDimensionRatio="1:1"
        android:text="7"
        android:textSize="10sp"
        android:gravity="center"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/pencilEight"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/pencilFour" />

    <TextView
        android:id="@+id/pencilEight"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintDimensionRatio="1:1"
        android:text="8"
        android:textSize="10sp"
        android:gravity="center"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/pencilNine"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/pencilSeven"
        app:layout_constraintTop_toBottomOf="@+id/pencilFive" />

    <TextView
        android:id="@+id/pencilNine"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintDimensionRatio="1:1"
        android:text="9"
        android:textSize="10sp"
        android:gravity="center"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/pencilEight"
        app:layout_constraintTop_toBottomOf="@+id/pencilSix" />

    <TextView
        android:id="@+id/pencilFive"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintDimensionRatio="1:1"
        android:text="5"
        android:textSize="10sp"
        android:gravity="center"
        android:visibility="invisible"
        app:layout_constraintBottom_toTopOf="@+id/pencilEight"
        app:layout_constraintEnd_toStartOf="@+id/pencilSix"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/pencilFour"
        app:layout_constraintTop_toBottomOf="@+id/pencilTwo" />
</android.support.constraint.ConstraintLayout>



from Application doing too much work in main thread loading the xml layout file

No comments:

Post a Comment