I am trying to create a very simple grid of buttons. I want this grid to be dynamic in size, that is I'd like to be able to set it to be 10x10 one day and maybe 5x5 the next.
This means that I cannot create the layout in an xml file. So far I have tried tried using a grid layout or a table layout. Both aren't doing the trick. I set the layout in the xml, like this:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_battle_ship_game"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context="com.example.admin.simplesimon.battleShip.BattleShipGame"
android:columnCount="10"
android:rowCount="10"
android:layout_gravity="center">
</TableLayout>
I then get the layout in my code and run a loop that inserts the tiles into it, my tiles are an extended Button class.
When I tried to use GridLayout I ended up with this:
It overflows and I have not been able to find a solution that works.
When I tried the TableLayout I could not (in code) set the number of columns and ended up with this, one column:
How is this kind of thing usually done? Surely there must be a simple way to create a grid of changing size. Something that will fit itself to the screen, stretching or shrinking the slots of the grid/table to allow for it to fit. I have seen posts about stretching the tiles to fill the screen when there are not enough of them but I cannot find a solution for the opposite.