Search code examples
javaandroiddynamicwidthtablerow

How do I get the table width of a dynamically created table?


i'm working on an app for language studying. I want to display the translations directly underneath the foreign language words. I currently try to accomplish this by using a dynamically created table. But the table gets wider then display width and a lot of the table is out of view. I want to test if words will be put out of View and in this case continue writing these words in a new table beneath.

How can I get the width of a table during runtime?

I'm new to android programming. Do you maybe have a better Idea how to show the diffenternt language textes on screen?

Resources res  = getResources();
        cn = res.getStringArray(R.array.lesson5_1_HanYu);
        py = res.getStringArray(R.array.lesson5_1_pinyin);
        dt = res.getStringArray(R.array.lesson5_1_deutsch);

        Intent in = getIntent();
        int index = in.getIntExtra("com.example.ITEM_INDEX", -1);

        TableLayout tableLayout;
        TableRow tableRow1, tableRow2, tableRow3;
        TextView cn1TV, py1TV, dt1TV, cn2TV, py2TV, dt2TV, cn3TV, py3TV, dt3TV;
        TextView[] cnTV = new TextView[cn.length];
        TextView[] pyTV = new TextView[py.length];
        TextView[] dtTV = new TextView[dt.length];

        if(index > -1){

            tableLayout = new TableLayout(this);
            tableRow1 = new TableRow(this);
            tableRow2 = new TableRow(this);
            tableRow3 = new TableRow(this);
            TableLayout.LayoutParams lp = new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

            int i = 0;
            boolean lineEnd = false;
            while(i < cn.length && lineEnd == false){
                cnTV[i] = new TextView(this);
                cnTV[i].setText(cn[i] + " ");
                pyTV[i] = new TextView(this);
                pyTV[i].setText(py[i] + " ");
                dtTV[i] = new TextView(this);
                dtTV[i].setText(dt[i] + " ");

                tableRow1.addView(cnTV[i]);
                tableRow2.addView(pyTV[i]);
                tableRow3.addView(dtTV[i]);

                // Check if next string is still in DisplayView area:

                // Code ...
                // change value of lineEnd if necessary


                i++; // proceed with next element in next interation
            }

            tableLayout.addView(tableRow1);
            tableLayout.addView(tableRow2);
            tableLayout.addView(tableRow3);

            setContentView(tableLayout);

        }

Solution

  • This is a code sample from one of my projects where I measure a textview. You would measure a TableRow

     TextView filter = findViewById(R.id.list_filter);
               filter.measure(0, 0);
    
               int center = filter.getLeft() + filter.getMeasuredWidth()/2;
    
                Point p =  new  ViewTarget(filter).getPoint();
    
    
    
    
                        Log.i(TAG, "x: " + p.x
                        + " width: " + filter.getWidth()
                        + " right: " + filter.getRight()
                        + " left: " + filter.getLeft()
                        + " measured: " + filter.getMeasuredWidth()
                );
    
                Log.i(TAG,  "value: " + (0- filter.getLeft() - filter.getMeasuredWidth()));