0

I am trying to find out how can somebody make a TableView without specifing it in an XML file. I have two activities:

  1. Takes the number of rows and columns
  2. Makes a TableView with the given rows and columns (the cells are filled with random numbers between 0 - 2)

So, this one workes fine, but when i'm trying to put too many columns, obviously the numbers are barely seen.

The second activity:

    public class TableCreator extends Activity {

    private String getRandomPoints(){
        Random rn = new Random();

        return new Integer(Math.abs(rn.nextInt()%3)).toString();
    }

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

        Bundle extras = getIntent().getExtras();
        int rows = extras.getInt("rows");
        int columns = extras.getInt("columns");


        TableLayout table = (TableLayout) findViewById(R.id.TableLayout);
        for(int i=0;i<rows;i++){
            TableRow row = new TableRow(this);
            row.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

            for(int j=0;j<columns;j++){
                TextView tv = new TextView(this);
                tv.setText(getRandomPoints());
                tv.setLayoutParams(new TableRow.LayoutParams(0,LayoutParams.WRAP_CONTENT,1));
                tv.setGravity(Gravity.CENTER);

                row.addView(tv);
            }
            table.addView(row);
        }
    }
}

And the XML File:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/TableLayout"
    android:stretchColumns="0,1"
    android:layout_weight="1"    
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</TableLayout> 

After that I tried to add a HorizontalScrollView in the xml file, so the TableView to be inside of it. This one was raising an exception, because the system couldn't decide the excact width an height that the TableLayout would use...

I also tried to add a HorizontalScrollView programmatically, but the numbers didn't show up. I can say that I searched carefully in this site about same questions, but still the answers are a little bit irrelevant to mine...

1 Answers1

0

Here ya go you can set the Layout as a horizontal scroll view and the tablelayout is the child

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".EvacRouteTableActivity" >

<TableLayout android:id="@+id/TableLayout01" android:layout_width="match_parent" android:layout_height="wrap_content"  android:stretchColumns="0">

  <TableRow 
      android:id="@+id/TableRow01" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content">
    <TextView 
        android:id="@+id/TextView01" 
        android:background="@drawable/cell_shape"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text=" Evacuation Routes (To view route click name)"
        android:width="340dp"
        android:height="30dp"
        android:textStyle="bold"></TextView>

  </TableRow>

</TableLayout>  
yams
  • 942
  • 6
  • 27
  • 60
  • Thank you for answering, but I can't see how you explain me what I asked... I want to understand the way that a HorizontalScrollView works with dynamically added data... The shape that you proposed me is helpful, but for another question... – Michael Bakogiannis Jul 02 '13 at 10:29
  • Ok changed my answer sorry about that. – yams Jul 02 '13 at 14:05
  • Thanks again for posting, but I say again that the textviews are added programmaticlly. The "non-dynamic" way is easy and can be done in no time... In order to save everyones time, I found the solution. In some words the problem doesn't seem to be because of the addition of the scrollView, but the way I was adding the LayoutParameters was wrong... Anyway see the [link](http://stackoverflow.com/questions/11963465/android-layoutparams-for-textview-makes-the-view-disappear-programatically) – Michael Bakogiannis Jul 02 '13 at 17:20
  • I do in my activity as well. – yams Jul 02 '13 at 17:42