Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Border Lines for cells in GridLayout, TableLayout, or GridView?

I am trying to create a table/grid for some items within my app and I would like to have a border around each cell to divide the items up and have a coherent association of the setting with the item. This app will be used in an industrial setting where there may be people unfamiliar with Android that need to use this, thus trying to make it as easy as possible.

The table/grid will contain TextView, EditText, Spinner, and Button, and will also be scrollable (via ScrollView parent).

I read about the GridView and found that it (seems) to only be able to get items programmatically, please correct me if I am wrong. I felt that this was unnecessary since I know what items I want and where. Also, I have not tried adding items to a layout programmatically yet so I figured I would try the other options first. Also, the GridView documentation does not say one way or the other if border lines are automatically shown, or if you can have them shown at all.

I started with a TableLayout and was able to get everything except the border lines to work. I tried android:divider to get the lines but that didn't work. One thought I had was to create a bunch of TextViews with black backgrounds and ~2dp widths/heights to make my own border lines. This feels like a huge waste though. Then I also read the TableLayout documentation and found this: "TableLayout containers do not display border lines for their rows, columns, or cells."

I then tried the GridLayout and had the same results as the TableLayout. I tried padding and margins, neither worked. Also, the GridLayout documentation states: "The grid is composed of a set of infinitely thin lines that separate the viewing area into cells."

My questions are:

  1. Is there an attirbute that I missed in TableLayout or GridLayout that will give me border lines via the xml?

  2. If no, then will the GridView give me the lines I want?

  3. Will I be able to add all the perviously mentioned items I want to the GridView?

like image 760
TronicZomB Avatar asked Apr 24 '13 18:04

TronicZomB


2 Answers

I was actually able to achieve the desired look by setting the android:background="#000000" within the GridLayout view and then in the child items I set the android:background="#8CDD81" (just some green color) and combined with android:layout_margin="2dp" I was able to get the "grid" lines that I wanted. Thanks to CommonsWare though for getting me thinking in a new direction that turned into a solution.

EDIT: This does not work quite as anticipated. You need the android:layout_alignLeft/Right which are only available via RelativeLayout in order to get just the right width on the child items. Haven't tested this yet using this idea, child items within RelativeLayout within GridLayout.

like image 109
TronicZomB Avatar answered Nov 17 '22 09:11

TronicZomB


Is there an attirbute that I missed in TableLayout or GridLayout that will give me border lines via the xml?

No.

If no, then will the GridView give me the lines I want?

No.

Will I be able to add all the perviously mentioned items I want to the GridView?

Yes, though how well something like a Spinner will work, I can't say.

The simplest way, off the top of my head, to give you the lines you seek is to have each cell of the TableLayout or GridLayout be some container containing the widget(s) for that cell, where you give the container a background that is your line. A ShapeDrawable could be defined in XML for that background, which will be nicely resizeable based upon the actual requirements of the cell.

like image 4
CommonsWare Avatar answered Nov 17 '22 07:11

CommonsWare