1

I want this table to extend as possible to fill the remaining (empty) area of the linear layout that contains it

enter image description here

I generate the table using the following method

public void drawTable(LinearLayout tableLayout,String tableTitle, String[][] data,String[] headerTitles, int type){

        //clearing previous views
         tableLayout.setVisibility(View.VISIBLE);
         tableLayout.setGravity(Gravity.CENTER);
         int chids = tableLayout.getChildCount();
         if (chids > 0){
             tableLayout.removeAllViews();
         }

        if((headerTitles!= null) && (headerTitles.length != data[0].length) ){
            return;
        }

        TableLayout table = new TableLayout(context);  
//      table.setStretchAllColumns(true);  
//      table.setShrinkAllColumns(true);  


        //Rendering the table title
        TextView tableTitleView = new TextView(context);
        tableTitleView.setText(tableTitle);
        tableTitleView.setTextColor(Color.WHITE);
        tableTitleView.setGravity(Gravity.CENTER);
        tableTitleView.setTextSize(18);
        tableTitleView.setTextAppearance(context, R.style.boldText);
        tableTitleView.setBackgroundColor(Color.parseColor("#008888"));
        tableTitleView.setPadding(2, 2, 2, 10);
        tableLayout.addView(tableTitleView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));








        ScrollView scroll1 = new ScrollView(context);
        HorizontalScrollView scroll2 = new HorizontalScrollView(context);


        int rows = data.length;
        int columns = data[0].length;

        if(headerTitles!=null){
            //Rendering the header
            TableRow headerRow = new TableRow(context);
            for (int i = 0; i < columns; i++) {
                TextView t = new TextView(context);
                t.setTextSize(17);
                t.setTextAppearance(context, R.style.boldText);
                t.setPadding(5, 5, 5, 5);
                t.setText(headerTitles[i]);
                t.setGravity(Gravity.CENTER);
                t.setTextColor(Color.WHITE);
                t.setBackgroundResource(R.drawable.text_border_header_1);
                headerRow.addView(t, LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
            }
            table.addView(headerRow, new TableLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        }

        //Rendering the Table Data
        TableRow row;
        for (int current = 0; current < rows ; current++) {
            row = new TableRow(context);
            TextView t;
            for(int i =0 ; i < columns ; i++){
                t = new TextView(context);
                t.setTextSize(15);
                t.setPadding(5,5,5,5);
                t.setText(data[current][i]);
                t.setGravity(Gravity.CENTER);

                if (type == 1) {
                    if (current % 2 == 0) {
                        t.setBackgroundResource(R.drawable.text_border_odd);
                    } else {
                        t.setBackgroundResource(R.drawable.text_border_even);
                    }
                }else if (type == 2) {
                    if (current % 2 == 0) {
                        t.setBackgroundResource(R.drawable.text_border_odd_2);
                    } else {
                        t.setBackgroundResource(R.drawable.text_border_even_2);
                    }
                }else if (type == 3) {
                    if (current % 2 == 0) {
                        t.setBackgroundResource(R.drawable.text_border_odd_3);
                    } else {
                        t.setBackgroundResource(R.drawable.text_border_even_3);
                    }
                }

                t.setTextColor(Color.BLACK);
                row.addView(t,TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
            }

            table.addView(row, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        }
        scroll2.addView(table,LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
        scroll1.addView(scroll2,LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
        scroll1.setPadding(2, 20, 2, 20);

        tableLayout.addView(scroll1,LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);


    }

I couldn't figure out exactly the problem with my code.

EDIT :

when I added the table directly to the linear layout instead of the scroll views the table fits with the linear layout width. It seems that my problem is with the scroll view

Abdelwahed
  • 1,694
  • 4
  • 21
  • 31

2 Answers2

1

I found the answer Here (thanks to Felix)

Just a single line of code to be added to the scroll views

scrollView.setFillViewPort(true);
Community
  • 1
  • 1
Abdelwahed
  • 1,694
  • 4
  • 21
  • 31
0

try this on your TableLayout object.

TableLayout.LayoutParams params = new TableLayout.LayoutParams();
params.width = LayoutParams.FILL_PARENT;
table.setLAyoutParams(params);

Edit:

TextView tView = new TextView(this);
LinearLayout.LayoutParams params = tView.getLayoutParams();
params.width = LayoutParams.FILL_PARENT;
params.weight = 1;
tView.setLayoutParams(params);

regards, Aqif

Aqif Hamid
  • 3,511
  • 4
  • 25
  • 38
  • check my edit! add this for every TextVIew that you are adding to your TableRows or Headers. edit: that weight param will be key to equal division of screen between row items. – Aqif Hamid Jun 27 '12 at 15:38
  • I changed all the layout params in the code to FILL_PARENT and added your code but no change at all. The same appearance of the table still. – Abdelwahed Jun 28 '12 at 07:52