Home Tutorials Hints & Tips

Getting Started in Android Development

Part 19 - The GridLayout Space Attribute

In order to complete the springboard menu we need to look at a technique to evenly distribute the image buttons on the screen. This can be achieved using the XML space attribute within the grid layout. A little more on that later on.

First of all, rather than using column and row count attribute values of 2, this modified layout will use a single column and so the column count attribute is set to 1.

android:columnCount="1"

The row count attribute is removed as it is not needed.

We want to have two rows, each of a single view correlating with the single column. We do this by using a linear layout with horizontal orientation for each row. Within each linear layout we place two of the image buttons with space attributes before the first, in between the two and after the second image button. The space attribute places white space in between views where used. Each of the space attributes in our example is given a weight of 1. This means each of the three spaces will be the same. Assigning a weight of say 2 to one space attributes would mean that that space attribute will be twice as great as the others.

If we run the app now, the first row of buttons will appear at the top of the screen and there will be no space in between the rows. We need to put some top margin into each linear layout to space out the rows. After this tutorial your activity_gridlayout.xml file should look similar to the one below. The GridLayoutActivity.java, ListviewActivity.java and Android Manifest files are unchanged from the last tutorial.

The resultant screenshot is shown below:

The basic android gridlayout

Download Download tutorial set 1

Privacy and Cookies Disclaimer Copyright
© 2015 - 2018 North Border Tech Training All rights reserved