Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Creating a Grid Layout in Appcelerator

I want to create a grid layout, similar to how the Facebook app opening screen is on the iPhone.
Is this possible? A 3x3 grid would be perfect.
Any ideas appreciated.
James

— asked June 21st 2010 by James Sugrue
  • iphone
1 Comment
  • We've been looking at the three twenty framework (based on the original facebook code) to accomplish this-my industry is full of applications with this sort of layout on the iPhone. On Android, it seems fairly similar to GridView. Any chance of getting this in Titanium Mobile? Or is there a way to do this now that I'm not aware of (I don't see anything like a grid view in the API and am a little reluctant to do this in WebView-it would sort of defeat the point of going with Titanium).

    — commented July 2nd 2010 by Brian Ferris

5 Answers

  • For those that need a cross-platform grid layout can use this code as a starting point:

    // to fit in a 320-wide space 
    var cellWidth = 92;
    var cellHeight = 92;
    var xSpacer = 10;
    var ySpacer = 10;
    var xGrid = 3;
    var yGrid = 12;
    
    var tableData = [];
    
    var colorSet = [
                    "#D44646",
                    "#46D463",
                    "#46D4BE",
                    "#C2D446",
                    "#D446D5",
                    "#4575D5",
                    "#E39127",
                    "#879181",
                    "#E291D4"
                  ];
    
    var colorSetIndex = 0;
    var cellIndex = 0;
    
    for (var y=0; y<yGrid; y++){
        var thisRow = Ti.UI.createTableViewRow({
            className: "grid",
            layout: "horizontal",
            height: cellHeight+(2*ySpacer),
            selectedBackgroundColor:"red"
        });
        for (var x=0; x<xGrid; x++){
            var thisView = Ti.UI.createView({
                objName:"grid-view",
                objIndex:cellIndex.toString(),
                backgroundColor: colorSet[colorSetIndex],
                left: ySpacer,
                height: cellHeight,
                width: cellWidth
            });
    
            var thisLabel = Ti.UI.createLabel({
                color:"white",
                font:{fontSize:48,fontWeight:'bold'},
                text:cellIndex.toString(),
                touchEnabled:false
            });
            thisView.add(thisLabel);
            thisRow.add(thisView);
            cellIndex++;
            colorSetIndex++;
    
            if( colorSetIndex === colorSet.length ){
                colorSetIndex = 0;
            }
        }
        tableData.push(thisRow);
    }
    var tableview = Ti.UI.createTableView({
        data:tableData
    });
    
    tableview.addEventListener("click", function(e){
        if(e.source.objName){
            Ti.API.info("---> " + e.source.objName+e.source.objIndex + " was clicked!");
        }
    });
    
    var win = Ti.UI.createWindow({
        backgroundColor:"black",
        navBarHidden:false,
        title:"Main Window"
    });
    win.add(tableview);
    
    win.open();
    

    screenshot

    — answered April 12th 2011 by Paul Dowsett
    permalink
    4 Comments
    • That worked like a charm, Cheers!

      — commented April 27th 2011 by Matt Tabor
    • How do you handle on orientationchange event?

      — commented August 19th 2011 by Dominik Koscielak
    • nice work

      — commented September 20th 2012 by Visuddha Karunaratne
    • Thanks! Its work like i want!!

      It can possible that each image-view or cell of table view will clickable??

      — commented October 3rd 2012 by Dhananjay Choudhari
  • Just use vertical layout, have three views stacked vertically on top of each other, with each view containing an icon aligned left, center and right. That's what I do.

    — answered November 28th 2010 by Eamonn Hynes
    permalink
    0 Comments
  • I created a empty project and in my app.js i paste the code. But when I run the iphone e iPad simultor, they crash… On the Android simulator the app run correctly…

    — answered May 11th 2011 by Francesco Aiello
    permalink
    0 Comments
  • have you tried the DashboardView UI Element

    — answered November 28th 2010 by Aaron Saunders
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.