0

I am attempting to make an 8x8 matrix of coloured squares on a black background in a layout that is limited to portrait only. I need the squares to all be equal in size, and the overall matrix to be 60% of the screen's width and of equal height. I also need to control the colour of each square dynamically as a result of my code, call it a pattern setting code if you will. Here is an example of what I need the final product to look like:

Screen example

I have been researching different ways of doing this and was hoping for some advice on the best approach. Here are some ideas I had:

1) Use GridLayout (not GridView) and set the background of each cell to the required colour. This shouldn't be difficult to control with java, but how do I enforce the correct dimensions? Would I need to fill each grid cell with something of a certain size, or can I use layout_weight attributes to control the dimensions?

2) Use a combination of RelativeLayout and LinearLayout with ImageView and display locally saved images of coloured squares. Can I use layout_weight to control the size of these images, and if these images were too small would they be stretched to fill their required dimensions? (I'm thinking of larger tablet screens.)

What I really need from someone is some sample code to implement a very simple version of what I need (for example one square in the centre of the screen that I can colour-control dynamically). Obviously I can then expand it to my specific requirements. If more details or code are required I will happily post them.

David
  • 169
  • 3
  • 13
  • I think you are looking for GridView indeed and [this](http://stackoverflow.com/questions/6557516/making-grid-view-items-square) solution. – Joel Bodega Mar 28 '13 at 17:38

1 Answers1

0

Perhaps you could just create a custom View object, and render the appropriate bitmap in the onDraw method? Seems like overkill to use layout objects, unless you're planning to do more than just render squares of colour?

Edit: Alternatively, I once used the info in the following posting to programmatically build a layout grid. It's not exactly what you need (it generates as many horizontal 'cells' as are necessary), but should make for insightful reading on manipulating layout dimensions:

Line-breaking widget layout for Android

Community
  • 1
  • 1
Barry O'Neill
  • 435
  • 6
  • 16
  • onDraw? I haven't used this before. I can use that to draw my matrix of 64 coloured squares and then just display it in the centre of the screen? – David Mar 28 '13 at 14:08
  • Check out [Shape Drawable Example on Android Dev Guide](http://developer.android.com/guide/topics/graphics/2d-graphics.html#shape-drawable) for a simple example on creating a custom view. The cool thing about this is that you can apply the standard XML layout params for width, centering etc, while only dealing with the contents themselves in your View implementation. You can also define extra XML params in your view that can be added. – Barry O'Neill Mar 28 '13 at 15:58
  • I've been looking at onDraw and it seems like it'll do the job for me. Thanks! – David Mar 28 '13 at 17:33