Programmatically created GridView cells don't scale to fit screen
- by ChrisAshton84
I've read a ton of other responses about GridView already but almost all deal with the XML format (which I had working). I wanted to learn the programmatic way of designing Android, though, so I'm trying to build most of this app without XML. All I define in XML are the GridView and the first TextView. After that I add the other LinearLayouts in onCreate().
I would like to have a 2 column GridView containing a title and several (4 for now) LinearLayouts. I realize from documentation that the GridView won't scale cells unless they have a gravity set, but no matter how I try to do this I can't get it to work.
After adding two cells, my GridView tree would look like:
GridView
-> TextView (colspan 2)
-> LinearLayout (Vertical)
-> TextView
-> LinearLayout (Horizontal)
-> TextView
-> TextView
-> LinearLayout (Horizontal)
-> TextView
-> TextView
-> LinearLayout (Vertical)
-> TextView
-> LinearLayout (Horizontal)
-> TextView
-> TextView
-> LinearLayout (Horizontal)
-> TextView
-> TextView
I've tried about every combination of FILL and FILL_HORIZONTAL I could think of on either the outermost LinearLayouts, or also trying on the TextViews and inner LinearLayouts. No matter what I do, the LinearLayouts I add are always sized as small as possible and pushed to the left of the screen.
Meanwhile, the first TextView (the colspan 2 one) with only CENTER_HORIZONTAL set is correctly centered in the screen. Its as if that TextView gets one idea of the column widths and the LinearLayouts get another! (If I add the FILL Gravity for it, it also moves all the way left.)
I believe I had this working accidentally with 100% XML, but I would prefer not to switch back unless this is known to not work programatically. Any ideas what I can try to get this working?