I am trying to find out how can somebody make a TableView without specifing it in an XML file. I have two activities:
- Takes the number of rows and columns
- 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...