Search code examples
androidandroid-layoutandroid-tablelayoutandroid-scrollviewandroid-scroll

Android horizontal and vertical layout development


This is a stupid question but i am in middle of an application in which i want to develop a layout as :

Layout

This layout scrolls vertically as well as horizontally. I thought to take a scroll view and under that table view, but how to make it scrollable horizontally also. The data is coming from the web service so I don't know what amount of data is going to come.

So please guide me.


Solution

  • This is not a good idea to use two scroll view. But if it is needed you can do one thing :

    Create XML layout as :

    <ScrollView
        android:id="@+id/scrollview_vertical_table"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
    
            <HorizontalScrollView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                tools:ignore="UselessParent" >
    
                <TableLayout
                    android:id="@+id/tablelayout"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:stretchColumns="*" >
    
                </TableLayout>
            </HorizontalScrollView>
        </LinearLayout>
    </ScrollView>
    

    And insert records or rows programmatically in this table as :

    TableLayout tableLayoutTransactionLineHeading = (TableLayout) findViewById(R.id.tablelayout);
    for(int j=0; j<rows; j++){      
        TableRow tableRow = new TableRow(this);
        tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
    
        TextView lineno = new TextView(this);
        lineno.setText(lineNo[j]);
        lineno.setPadding(10, 10, 10, 10);
        lineno.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
        tableRow.addView(lineno);
    
        View v = new View(this);
                v.setLayoutParams(new TableRow.LayoutParams(1, TableRow.LayoutParams.MATCH_PARENT));
                v.setBackgroundColor(Color.rgb(50, 50, 50));
                tableRow.addView(v);
    
        tableLayout.addView(tableRow, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT,0f));
    }
    

    Hope this will help you.