How to create a 10x10 grid in Android?

I am creating a small Battleship game and for Android UI I was hoping to create a grid of 10x10 buttons with an image or something (something simple). I followed the tutorial presented in this link

My problem is that I cannot get the images to display in the correct 10x10 format. Using a GridView, I set num_columns to "10" in XML, but there is no such attribute for the number of rows. Since phone sizes vary, is there a way to set up a 10x10 grid on any screen?

Following the steps in the link above, I can display 100 clickable images, but my problem is with formatting. Here is a screenshot of the result: enter image description here

I tried changing the values ​​in XML to scale the images down, but cannot figure out how to do this. Apologies for the lack of code snippets, but I really haven't changed much compared to the sample shown in the link above, different from vertical and horizontal intervals.

I would thank for any enlightenment, even if he simply said that I would completely change that.

+4
source share
3 answers

You can achieve this with a table layout: http://www.mkyong.com/android/android-tablelayout-example/

GridViews, like ListViews, were designed to contain any number of rows.

But I really doubt that you want to use representations in your game, and not just use the canvas, draw anything and detect clicks without relying on the built-in presentation mechanisms. This works more, but you end up making you much freer to do what you want (for example, not to deal with the number of lines of the container ...)

+3
source

go to this link and try the example of the 2nd example Custom Adapter. just change android: numColumns = "auto_fit" to android: numColumns = "10".

<?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridView1" android:numColumns="10" android:gravity="center" android:columnWidth="100dp" android:stretchMode="columnWidth" android:layout_width="fill_parent" android:layout_height="fill_parent" > </GridView> 
+1
source

Perhaps you can do what you want with a GridLayout , but not with a GridView (which automatically scrolls vertically). Using the GridLayout , set the width and height of the layout to 0dp for all buttons and set their density to fill .

0
source

All Articles