2

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:enter image description here

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:

enter image description here

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.

TheFooBarWay
  • 594
  • 1
  • 7
  • 17
  • You could easily achieve that using a `RecyclerView` with a `GridLayoutManager`. – Budius Oct 08 '16 at 19:14
  • Isn't that meant for creating scroll-able lists in the form of a grid? From what I remember the whole point of the Recycle aspect is to allow for very long lists without holding a huge amount of components in a large list. – TheFooBarWay Oct 08 '16 at 19:37
  • That is true, but even if the amount of components fitting into just 1 screen, the fact that those components are all exactly the same, the code using RecyclerView is much simpler and wel separated and you won't be using any more resources that you would with a cumbersom TableLayout or GridView – Budius Oct 08 '16 at 19:41
  • Yes, but what I worry about is what will happen when I want to enlarge the grid to be 20X20. It might no longer fit the screen and then it would start scrolling. What I am looking for is a layout that will force it's tiles to scale down. – TheFooBarWay Oct 08 '16 at 19:44
  • For that you can extend your ViewGroupd to add `max_height`. And make that the root of each recycler item. And then set `maxHeight = recyclerHeight / numRows`. Here is how to implement `max_height` http://stackoverflow.com/questions/4054567/android-why-is-there-no-maxheight-for-a-view – Budius Oct 08 '16 at 19:48
  • I will admit that I haven't gone very deep into the link you posted yet, I will try it when time permits, but even at first glance it seems like an awfully complicated way to do this sort of thing. Could be the standard way to do it for all I know, I was just hoping that a simple stretching and shining grid would be easier to create. – TheFooBarWay Oct 09 '16 at 07:01
  • I totally understand your point. I wrote RecyclerView libraries, so for me anything that repeats the same layout I'm biased to applying RV. That's also the reason I wrote all that as comments and not an answer. Best of luck then. – Budius Oct 09 '16 at 19:46

0 Answers0