R
R
relgames2011-11-03 23:16:06
Android
relgames, 2011-11-03 23:16:06

Android development: draw a table with scrolling vertically and horizontally?

There is a task: to draw a table based on a two-dimensional list, the table should automatically adjust to the size of the text in the cells. The table may not fit on the screen, it must be scrollable vertically and horizontally.
So far, it turned out to be written like this (in the cycle I go through the columns, inside each column in the cycle I go through the cells):

LinearLayout table = new LinearLayout(this);
        table.setOrientation(LinearLayout.HORIZONTAL);

        for (LevelColumn level : statistics.levels) {
            LinearLayout column = new LinearLayout(this);
            column.setOrientation(LinearLayout.VERTICAL);

            TextView header = new TextView(this);
            header.setText(level.name);

            column.addView(header);

            for (Score score : level.scores) {
                TextView cell = new TextView(this);
                cell.setText(score.teamName);

                column.addView(cell);
            }

            table.addView(column);
        }


        TwoDScrollView scrollView = new TwoDScrollView(this);
        scrollView.addView(table);

        setContentView(scrollView);

TwoDScrollView found here blog.gorges.us/wp-content/uploads/TwoDScrollView.java
As a result, the table is drawn, scrolled, but the size of the columns does not adjust to the text, instead, the columns are drawn with a fixed width.
I also can not figure out how to center the text in the cells horizontally.
I will be grateful for any advice.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
doubt, 2011-11-03
@doubt

Perhaps this will help: If it does not help, then for the cell, similarly specify the wrap content parameter.
LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
column.setLayoutParams(params);

O
OlegTar, 2013-12-24
@OlegTar

you can make your own custom view and use canvas
Sorry, but I give the code in c #
Use commands like
A to find out the size of the text, like this:

Rect bounds = new Rect();
p.GetTextBounds (text, 0, text.Length, bounds);

where p is a variable of type Paint
An example of creating your own View here Android: strange behavior of Canvas ( you need to open the "code" link)
and here You can contact with me any time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question