These code examples were ranked by Codota’s semantic indexing as the best open source examples for GridLayout setColumnCount method.
Spec col0 = GridLayout.spec(0); Spec col1 = GridLayout.spec(1); Spec colspan2 = GridLayout.spec(0,4); GridLayout gridLayout = new GridLayout(this); gridLayout.setColumnCount(4); gridLayout.setRowCount(4); TextView twoByTwo1 = new TextView(this); GridLayout.LayoutParams first = new GridLayout.LayoutParams(row1, colspan2); first.width = screenWidth; first.height = halfScreenHeight; twoByTwo1.setLayoutParams(first); twoByTwo1.setGravity(Gravity.CENTER); twoByTwo1.setBackgroundColor(Color.RED); twoByTwo1.setText("TOP"); twoByTwo1.setTextAppearance(this, android.R.style.TextAppearance_Large); gridLayout.addView(twoByTwo1, first);
Spec col0 = GridLayout.spec(0); Spec col1 = GridLayout.spec(1); Spec colspan2 = GridLayout.spec(0,4); GridLayout gridLayout = new GridLayout(this); gridLayout.setColumnCount(4); gridLayout.setRowCount(4); TextView twoByTwo1 = new TextView(this); GridLayout.LayoutParams first = new GridLayout.LayoutParams(row1, colspan2); first.width = screenWidth; first.height = halfScreenHeight; twoByTwo1.setLayoutParams(first); twoByTwo1.setGravity(Gravity.CENTER); twoByTwo1.setBackgroundColor(Color.RED); twoByTwo1.setText("TOP"); twoByTwo1.setTextAppearance(this, android.R.style.TextAppearance_Large); gridLayout.addView(twoByTwo1, first); TextView twoByOne1 = new TextView(this); GridLayout.LayoutParams second = new GridLayout.LayoutParams(row2, GridLayout.spec(0,3)); second.width = (int) (screenWidth*0.75); second.height = quarterScreenHeight; twoByOne1.setLayoutParams(second); twoByOne1.setBackgroundColor(Color.BLUE); twoByOne1.setText("Staff Choices"); twoByOne1.setTextAppearance(this, android.R.style.TextAppearance_Large); gridLayout.addView(twoByOne1, second); TextView twoByOne2 = new TextView(this); GridLayout.LayoutParams third = new GridLayout.LayoutParams(row2, GridLayout.spec(3)); third.width = (int) (screenWidth*0.25); third.height = quarterScreenHeight; twoByOne2.setLayoutParams(third); twoByOne2.setBackgroundColor(Color.GREEN); twoByOne2.setText("Games"); twoByOne2.setTextAppearance(this, android.R.style.TextAppearance_Large); gridLayout.addView(twoByOne2, third); TextView twoByOne3 = new TextView(this); GridLayout.LayoutParams fourth = new GridLayout.LayoutParams(row3, GridLayout.spec(0,1)); fourth.width = (int) (screenWidth*0.25); fourth.height = quarterScreenHeight; twoByOne3.setLayoutParams(fourth); twoByOne3.setBackgroundColor(Color.YELLOW); twoByOne3.setText("Editor's Choices"); twoByOne3.setTextAppearance(this, android.R.style.TextAppearance_Large_Inverse); gridLayout.addView(twoByOne3, fourth); TextView twoByOne4 = new TextView(this); GridLayout.LayoutParams fifth = new GridLayout.LayoutParams(row3, GridLayout.spec(1,3)); fifth.width = (int) (screenWidth*0.75); fifth.height = quarterScreenHeight; twoByOne4.setLayoutParams(fifth); twoByOne4.setBackgroundColor(Color.MAGENTA); twoByOne4.setText("Something Else"); twoByOne4.setTextAppearance(this, android.R.style.TextAppearance_Large); gridLayout.addView(twoByOne4, fifth); TextView twoByTwo2 = new TextView(this); GridLayout.LayoutParams sixth = new GridLayout.LayoutParams(row4, colspan2); sixth.width = screenWidth; sixth.height = halfScreenHeight; twoByTwo2.setLayoutParams(sixth); twoByTwo2.setGravity(Gravity.CENTER); twoByTwo2.setBackgroundColor(Color.CYAN); twoByTwo2.setTextColor(Color.BLACK); twoByTwo2.setText("BOTTOM"); twoByTwo2.setTextAppearance(this, android.R.style.TextAppearance_Large_Inverse); gridLayout.addView(twoByTwo2, sixth); setContentView(gridLayout); } @Override
imuReset.setText("Reset IMU"); imuReset.setOnClickListener(this); // END ----- reset IMU -- // create GridLayout GridLayout grid = new GridLayout(context); grid.setColumnCount(2); grid.setRowCount(5); // add Views GridLayout.LayoutParams span2 = new GridLayout.LayoutParams(); span2.columnSpec = GridLayout.spec(0, 2); int widthColumn2 = 300; GridLayout.LayoutParams param1 = new GridLayout.LayoutParams(); param1.width = widthColumn2; GridLayout.LayoutParams param2 = new GridLayout.LayoutParams(); param2.width = widthColumn2; GridLayout.LayoutParams param3 = new GridLayout.LayoutParams();
imuReset.setText("Reset IMU"); imuReset.setOnClickListener(this); // END ----- reset IMU -- // create GridLayout GridLayout grid = new GridLayout(context); grid.setColumnCount(2); grid.setRowCount(5); // add Views GridLayout.LayoutParams span2 = new GridLayout.LayoutParams(); span2.columnSpec = GridLayout.spec(0, 2); int widthColumn2 = 300; GridLayout.LayoutParams param1 = new GridLayout.LayoutParams(); param1.width = widthColumn2; GridLayout.LayoutParams param2 = new GridLayout.LayoutParams(); param2.width = widthColumn2; GridLayout.LayoutParams param3 = new GridLayout.LayoutParams(); param3.width = widthColumn2; grid.addView(title, span2); grid.addView(labelCam); grid.addView(_sliderCam, param1); grid.addView(labelLeft); grid.addView(_sliderLeft, param2); grid.addView(labelRight); grid.addView(_sliderRight, param3); grid.addView(imuReset); // add grid-layout layout.addView(grid); }
157: public void setupVertical(){
158: size = getBaseSizeVertical();
159: this.setColumnCount(columns);
160: this.setRowCount(-1);
161: this.setOrientation(this.HORIZONTAL);
-
167: size = getBaseSizeHorizontal();
168: this.setRowCount(rows);
169: this.setColumnCount(-1);
170: this.setOrientation(this.VERTICAL);
171: FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
Thanks to Siddharth Lele for the Idea, so that I came to this solution. I am pasting the code which is structured. I got some problems while arranging it in a format, now i think others can have it with that tension. Just keep in mind that the code is just for 4.0 and above.
activity_main.xml
<com.jake.quiltviewsample.QuiltView
android:id="@+id/quilt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dip" >
</com.jake.quiltviewsample.QuiltView>
</FrameLayout>
MainActivity.java
public class MainActivity extends Activity {
public QuiltView quiltView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
quiltView = (QuiltView) findViewById(R.id.quilt);
quiltView.setChildPadding(5);
addTestQuilts(200);
}
public void addTestQuilts(int num){
ArrayList<ImageView> images = new ArrayList<ImageView>();
for(int i = 0; i < num; i++){
ImageView image = new ImageView(this.getApplicationContext());
image.setScaleType(ScaleType.CENTER_CROP);
if(i % 2 == 0)
image.setImageResource(R.drawable.mayer);
else
image.setImageResource(R.drawable.mayer1);
images.add(image);
}
quiltView.addPatchImages(images);
}
}
QuiltView.java
public class QuiltView extends FrameLayout implements OnGlobalLayoutListener {
public QuiltViewBase quilt;
public ViewGroup scroll;
public int padding = 5;
public boolean isVertical = false;
public ArrayList<View> views;
private Adapter adapter;
public QuiltView(Context context,boolean isVertical) {
super(context);
this.isVertical = isVertical;
setup();
}
public QuiltView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.QuiltView);
String orientation = a.getString(R.styleable.QuiltView_scrollOrientation);
if(orientation != null){
if(orientation.equals("vertical")){
isVertical = true;
} else {
isVertical = false;
}
}
setup();
}
public void setup(){
views = new ArrayList<View>();
if(isVertical){
scroll = new ScrollView(this.getContext());
} else {
scroll = new HorizontalScrollView(this.getContext());
}
quilt = new QuiltViewBase(getContext(), isVertical);
scroll.addView(quilt);
this.addView(scroll);
}
private DataSetObserver adapterObserver = new DataSetObserver(){
public void onChanged(){
super.onChanged();
onDataChanged();
}
public void onInvalidated(){
super.onInvalidated();
onDataChanged();
}
public void onDataChanged(){
setViewsFromAdapter(adapter);
}
};
public void setAdapter(Adapter adapter){
this.adapter = adapter;
adapter.registerDataSetObserver(adapterObserver);
setViewsFromAdapter(adapter);
}
private void setViewsFromAdapter(Adapter adapter) {
this.removeAllViews();
for(int i = 0; i < adapter.getCount(); i++){
quilt.addPatch(adapter.getView(i, null, quilt));
}
}
public void addPatchImages(ArrayList<ImageView> images){
for(ImageView image: images){
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
image.setLayoutParams(params);
LinearLayout wrapper = new LinearLayout(this.getContext());
wrapper.setPadding(padding, padding, padding, padding);
wrapper.addView(image);
quilt.addPatch(wrapper);
}
}
public void addPatchViews(ArrayList<View> views_a){
for(View view: views_a){
quilt.addPatch(view);
}
}
public void addPatchesOnLayout(){
for(View view: views){
quilt.addPatch(view);
}
}
public void removeQuilt(View view){
quilt.removeView(view);
}
public void setChildPadding(int padding){
this.padding = padding;
}
public void refresh(){
quilt.refresh();
}
public void setOrientation(boolean isVertical){
this.isVertical = isVertical;
}
@Override
public void onGlobalLayout() {
//addPatchesOnLayout();
}
}
**QuiltViewBase.java**
public class QuiltViewBase extends GridLayout {
public int[] size;
public int columns;
public int rows;
public int view_width = -1;
public int view_height = -1;
public boolean isVertical = true;
public ArrayList<View> views;
public QuiltViewBase(Context context, boolean isVertical) {
super(context);
this.isVertical = isVertical;
if(view_width == -1){
DisplayMetrics metrics = this.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels - 120;
view_width = width - this.getPaddingLeft() - this.getPaddingRight();
view_height = height - this.getPaddingTop() - this.getPaddingBottom();
}
views = new ArrayList<View>();
setup();
}
public void setup(){
if(isVertical){
setupVertical();
} else {
setupHorizontal();
}
}
public void setupVertical(){
size = getBaseSizeVertical();
this.setColumnCount(columns);
this.setRowCount(-1);
this.setOrientation(this.HORIZONTAL);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
this.setLayoutParams(params);
}
public void setupHorizontal(){
size = getBaseSizeHorizontal();
this.setRowCount(rows);
this.setColumnCount(-1);
this.setOrientation(this.VERTICAL);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
this.setLayoutParams(params);
}
public void addPatch(View view){
int count = this.getChildCount();
QuiltViewPatch child = QuiltViewPatch.init(count, columns);
GridLayout.LayoutParams params = new GridLayout.LayoutParams();
params.width = size[0]*child.width_ratio;
params.height = size[1]*child.height_ratio;
params.rowSpec = GridLayout.spec(Integer.MIN_VALUE, child.height_ratio);
params.columnSpec = GridLayout.spec(Integer.MIN_VALUE, child.width_ratio);
view.setLayoutParams(params);
addView(view);
views.add(view);
}
public void refresh(){
this.removeAllViewsInLayout();
setup();
for(View view : views){
addPatch(view);
}
}
public int[] getBaseSize(){
int[] size = new int[2];
float width_height_ratio = (3.0f/4.0f);
int base_width = getBaseWidth();
int base_height = (int) (base_width*width_height_ratio);
size[0] = base_width; // width
size[1] = base_height; // height
return size;
}
public int[] getBaseSizeVertical(){
int[] size = new int[2];
float width_height_ratio = (3.0f/4.0f);
int base_width = getBaseWidth();
int base_height = (int) (base_width*width_height_ratio);
size[0] = base_width; // width
size[1] = base_height; // height
return size;
}
public int[] getBaseSizeHorizontal(){
int[] size = new int[2];
float width_height_ratio = (4.0f/3.0f);
int base_height = getBaseHeight();
int base_width = (int) (base_height*width_height_ratio);
size[0] = base_width; // width
size[1] = base_height; // height
return size;
}
public int getBaseWidth(){
if(view_width < 500){
columns = 2;
} else if(view_width < 801){
columns = 3;
} else if(view_width < 1201){
columns = 4;
} else if(view_width < 1601){
columns = 5;
} else {
columns = 6;
}
return (view_width / columns);
}
public int getBaseHeight(){
if(view_height < 350){
rows = 2;
} else if(view_height < 650){
rows = 3;
} else if(view_height < 1050){
rows = 4;
} else if(view_height < 1250){
rows = 5;
} else {
rows = 6;
}
return (view_height / rows);
}
/*@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
view_width = parentWidth;
view_height = parentHeight;
setup(isVertical);
}*/
@Override
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld){
super.onSizeChanged(xNew, yNew, xOld, yOld);
view_width = xNew;
view_height = yNew;
}
}
**QuiltViewPatch.java**
public class QuiltViewPatch implements Comparable{
public int width_ratio;
public int height_ratio;
public QuiltViewPatch(int width_ratio, int height_ratio){
this.width_ratio = width_ratio;
this.height_ratio = height_ratio;
}
private static QuiltViewPatch create(Size size){
switch(size){
case Big:
return new QuiltViewPatch(2,2);
case Small:
return new QuiltViewPatch(1,1);
case Tall:
return new QuiltViewPatch(1,2);
}
return new QuiltViewPatch(1,1);
}
public int getHeightRatio(){return this.height_ratio;}
public int getWidthRatio(){return this.width_ratio;}
public static QuiltViewPatch create(int view_count){
if(view_count == 0)
return new QuiltViewPatch(2,2);
else if((view_count % 11) == 0)
return new QuiltViewPatch(2,2);
else if((view_count % 4) == 0)
return new QuiltViewPatch(1,2);
else
return new QuiltViewPatch(1,1);
}
private enum Size{
Big,
Small,
Tall
}
public static QuiltViewPatch init(int position, int column){
switch(column){
case 2:
return init2(position);
case 3:
return init3(position);
case 4:
return init4(position);
case 5:
return init5(position);
}
return init3(position);
}
private static QuiltViewPatch init2(int position){
switch(position % 15){
case 0:
return create(Size.Big);
case 1:
case 2:
case 3:
return create(Size.Small);
case 4:
return create(Size.Tall);
case 5:
case 6:
case 7:
return create(Size.Small);
case 8:
return create(Size.Tall);
case 9:
return create(Size.Tall);
case 10:
return create(Size.Small);
case 11:
return create(Size.Big);
case 12:
return create(Size.Tall);
case 13:
return create(Size.Tall);
case 14:
return create(Size.Small);
}
return create(Size.Small);
}
private static QuiltViewPatch init3(int position){
switch(position % 32){
case 0:
return create(Size.Big);
case 1:
case 2:
case 3:
return create(Size.Small);
case 4:
return create(Size.Tall);
case 5:
case 6:
case 7:
return create(Size.Small);
case 8:
return create(Size.Tall);
case 9:
case 10:
return create(Size.Small);
case 11:
return create(Size.Big);
case 12:
return create(Size.Tall);
case 13:
case 14:
return create(Size.Small);
case 15:
return create(Size.Small);
case 16:
return create(Size.Tall);
case 17:
case 18:
case 19:
return create(Size.Small);
case 20:
return create(Size.Tall);
case 21:
case 22:
return create(Size.Small);
case 23:
return create(Size.Big);
case 24:
return create(Size.Small);
case 25:
return create(Size.Tall);
case 26:
case 27:
case 28:
return create(Size.Small);
case 29:
return create(Size.Tall);
case 30:
case 31:
return create(Size.Small);
}
return create(Size.Small);
}
private static QuiltViewPatch init4(int position){
switch(position % 36){
case 0:
return create(Size.Big);
case 1:
case 2:
case 3:
return create(Size.Small);
case 4:
return create(Size.Tall);
case 5:
case 6:
case 7:
return create(Size.Small);
case 8:
return create(Size.Tall);
case 9:
case 10:
case 11:
return create(Size.Small);
case 12:
return create(Size.Big);
case 13:
return create(Size.Tall);
case 14:
case 15:
case 16:
return create(Size.Small);
case 17:
return create(Size.Tall);
case 18:
case 19:
case 20:
return create(Size.Small);
case 21:
return create(Size.Tall);
case 22:
case 23:
return create(Size.Small);
case 24:
return create(Size.Small);
case 25:
return create(Size.Big);
case 26:
return create(Size.Small);
case 27:
return create(Size.Tall);
case 28:
case 29:
case 30:
return create(Size.Small);
case 31:
return create(Size.Tall);
case 32:
case 33:
case 34:
case 35:
return create(Size.Small);
}
return create(Size.Small);
}
private static QuiltViewPatch init5(int position){
switch(position % 35){
case 0:
return create(Size.Big);
case 1:
case 2:
case 3:
return create(Size.Small);
case 4:
return create(Size.Tall);
case 5:
case 6:
case 7:
return create(Size.Small);
case 8:
return create(Size.Tall);
case 9:
case 10:
case 11:
return create(Size.Small);
case 12:
return create(Size.Big);
case 13:
return create(Size.Tall);
case 14:
case 15:
case 16:
return create(Size.Small);
case 17:
return create(Size.Tall);
case 18:
case 19:
case 20:
return create(Size.Small);
case 21:
return create(Size.Tall);
case 22:
case 23:
case 24:
return create(Size.Small);
case 25:
return create(Size.Big);
case 26:
return create(Size.Small);
case 27:
return create(Size.Tall);
case 28:
case 29:
case 30:
return create(Size.Small);
case 31:
return create(Size.Tall);
case 32:
return create(Size.Big);
case 33:
return create(Size.Tall);
case 34:
return create(Size.Small);
}
return create(Size.Small);
}
public static boolean getRandomBoolean(){
return (Math.random() < 0.5);
}
public boolean equals(Object obj){
if(obj != null && obj instanceof QuiltViewPatch){
QuiltViewPatch size = (QuiltViewPatch)obj;
return size.height_ratio == this.height_ratio && size.width_ratio == this.width_ratio;
}
return false;
}
public int hashCode(){
return height_ratio + 100 * width_ratio;
}
public String toString(){
return "Patch: " + height_ratio + " x " + width_ratio;
}
@Override
public int compareTo(Object another) {
if(another != null && another instanceof QuiltViewPatch){
QuiltViewPatch size = (QuiltViewPatch)another;
if(size.equals(this))
return 0;
if(this.height_ratio < size.height_ratio)
return -1;
else if(this.height_ratio > size.height_ratio)
return 1;
if(this.width_ratio < size.width_ratio)
return -1;
else
return 1;
}
return -1;
}
}
16:
17: GridLayout gridLayout = new GridLayout(this);
18: gridLayout.setColumnCount(2);
19: gridLayout.setRowCount(15);
20:
Note: The information below the horizontal line is no longer accurate with the introduction of Android 'Lollipop' 5, as GridLayout
does accommodate the principle of weights since API level 21.
Quoted from the Javadoc:
Excess Space Distribution
As of API 21, GridLayout's distribution of excess space accomodates the principle of weight. In the event that no weights are specified, the previous conventions are respected and columns and rows are taken as flexible if their views specify some form of alignment within their groups. The flexibility of a view is therefore influenced by its alignment which is, in turn, typically defined by setting the gravity property of the child's layout parameters. If either a weight or alignment were defined along a given axis then the component is taken as flexible in that direction. If no weight or alignment was set, the component is instead assumed to be inflexible.
Multiple components in the same row or column group are considered to act in parallel. Such a group is flexible only if all of the components within it are flexible. Row and column groups that sit either side of a common boundary are instead considered to act in series. The composite group made of these two elements is flexible if one of its elements is flexible.
To make a column stretch, make sure all of the components inside it define a weight or a gravity. To prevent a column from stretching, ensure that one of the components in the column does not define a weight or a gravity.
When the principle of flexibility does not provide complete disambiguation, GridLayout's algorithms favour rows and columns that are closer to its right and bottom edges. To be more precise, GridLayout treats each of its layout parameters as a constraint in the a set of variables that define the grid-lines along a given axis. During layout, GridLayout solves the constraints so as to return the unique solution to those constraints for which all variables are less-than-or-equal-to the corresponding value in any other valid solution.
It's also worth noting that android.support.v7.widget.GridLayout
contains the same information. Unfortunately it doesn't mention which version of the support library it was introduced with, but the commit that adds the functionality can be tracked back to July 2014. In November 2014, improvements in weight calculation and a bug was fixed.
To be safe, make sure to import the latest version of the gridlayout-v7 library.
The principle of 'weights', as you're describing it, does not exist with GridLayout
. This limitation is clearly mentioned in the documentation; excerpt below. That being said, there are some possibilities to use 'gravity' for excess space distribution. I suggest you have read through the linked documentation.
Limitations
GridLayout does not provide support for the principle of weight, as defined in weight. In general, it is not therefore possible to configure a GridLayout to distribute excess space in non-trivial proportions between multiple rows or columns. Some common use-cases may nevertheless be accommodated as follows. To place equal amounts of space around a component in a cell group; use CENTER alignment (or gravity). For complete control over excess space distribution in a row or column; use a LinearLayout subview to hold the components in the associated cell group. When using either of these techniques, bear in mind that cell groups may be defined to overlap.
For an example and some practical pointers, take a look at last year's blog post introducing the GridLayout
widget.
Edit: I don't think there's an xml-based approach to scaling the tiles like in the Google Play app to 'squares' or 'rectangles' twice the length of those squares. However, it is certainly possible if you build your layout programmatically. All you really need to know in order two accomplish that is the device's screen dimensions.
Below a (very!) quick 'n dirty approximation of the tiled layout in the Google Play app.
Point size = new Point();
getWindowManager().getDefaultDisplay().getSize(size);
int screenWidth = size.x;
int screenHeight = size.y;
int halfScreenWidth = (int)(screenWidth *0.5);
int quarterScreenWidth = (int)(halfScreenWidth * 0.5);
Spec row1 = GridLayout.spec(0, 2);
Spec row2 = GridLayout.spec(2);
Spec row3 = GridLayout.spec(3);
Spec row4 = GridLayout.spec(4, 2);
Spec col0 = GridLayout.spec(0);
Spec col1 = GridLayout.spec(1);
Spec colspan2 = GridLayout.spec(0, 2);
GridLayout gridLayout = new GridLayout(this);
gridLayout.setColumnCount(2);
gridLayout.setRowCount(15);
TextView twoByTwo1 = new TextView(this);
GridLayout.LayoutParams first = new GridLayout.LayoutParams(row1, colspan2);
first.width = screenWidth;
first.height = quarterScreenWidth * 2;
twoByTwo1.setLayoutParams(first);
twoByTwo1.setGravity(Gravity.CENTER);
twoByTwo1.setBackgroundColor(Color.RED);
twoByTwo1.setText("TOP");
twoByTwo1.setTextAppearance(this, android.R.style.TextAppearance_Large);
gridLayout.addView(twoByTwo1, first);
TextView twoByOne1 = new TextView(this);
GridLayout.LayoutParams second = new GridLayout.LayoutParams(row2, col0);
second.width = halfScreenWidth;
second.height = quarterScreenWidth;
twoByOne1.setLayoutParams(second);
twoByOne1.setBackgroundColor(Color.BLUE);
twoByOne1.setText("Staff Choices");
twoByOne1.setTextAppearance(this, android.R.style.TextAppearance_Large);
gridLayout.addView(twoByOne1, second);
TextView twoByOne2 = new TextView(this);
GridLayout.LayoutParams third = new GridLayout.LayoutParams(row2, col1);
third.width = halfScreenWidth;
third.height = quarterScreenWidth;
twoByOne2.setLayoutParams(third);
twoByOne2.setBackgroundColor(Color.GREEN);
twoByOne2.setText("Games");
twoByOne2.setTextAppearance(this, android.R.style.TextAppearance_Large);
gridLayout.addView(twoByOne2, third);
TextView twoByOne3 = new TextView(this);
GridLayout.LayoutParams fourth = new GridLayout.LayoutParams(row3, col0);
fourth.width = halfScreenWidth;
fourth.height = quarterScreenWidth;
twoByOne3.setLayoutParams(fourth);
twoByOne3.setBackgroundColor(Color.YELLOW);
twoByOne3.setText("Editor's Choices");
twoByOne3.setTextAppearance(this, android.R.style.TextAppearance_Large_Inverse);
gridLayout.addView(twoByOne3, fourth);
TextView twoByOne4 = new TextView(this);
GridLayout.LayoutParams fifth = new GridLayout.LayoutParams(row3, col1);
fifth.width = halfScreenWidth;
fifth.height = quarterScreenWidth;
twoByOne4.setLayoutParams(fifth);
twoByOne4.setBackgroundColor(Color.MAGENTA);
twoByOne4.setText("Something Else");
twoByOne4.setTextAppearance(this, android.R.style.TextAppearance_Large);
gridLayout.addView(twoByOne4, fifth);
TextView twoByTwo2 = new TextView(this);
GridLayout.LayoutParams sixth = new GridLayout.LayoutParams(row4, colspan2);
sixth.width = screenWidth;
sixth.height = quarterScreenWidth * 2;
twoByTwo2.setLayoutParams(sixth);
twoByTwo2.setGravity(Gravity.CENTER);
twoByTwo2.setBackgroundColor(Color.WHITE);
twoByTwo2.setText("BOTOM");
twoByTwo2.setTextAppearance(this, android.R.style.TextAppearance_Large_Inverse);
gridLayout.addView(twoByTwo2, sixth);
The result will look somewhat like this (on my Galaxy Nexus):