Border Lines for cells in GridLayout, TableLayout, or GridView?
Solution 1
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.
Solution 2
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
.
Solution 3
For future visiters this is how I did it with TableLayout
:
table.xml
<TableLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#969696">
<!-- table heading -->
<TableRow>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:background="#d2d2d2"
android:layout_margin="1dp"
/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"
android:background="#d2d2d2"
android:layout_margin="1dp"
/>
</TableRow>
<!-- table data -->
<TableRow>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ahtisham"
android:layout_margin="1dp"
android:background="#f1f1f1"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kashmir"
android:layout_margin="1dp"
android:background="#f1f1f1"
/>
</TableRow>
</TableLayout>
TronicZomB
SOreadytohelp Studied Electrical & Computer Engineering and Religious Studies. Check out my website for a free Notepad++ LogCat language to add some color to your exported LogCat text files in Notepad++.
Updated on July 15, 2022Comments
-
TronicZomB almost 2 years
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
, andButton
, and will also be scrollable (viaScrollView
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, theGridView
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 triedandroid:divider
to get the lines but that didn't work. One thought I had was to create a bunch ofTextView
s 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 theTableLayout
. I triedpadding
andmargins
, 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:
Is there an attirbute that I missed in
TableLayout
orGridLayout
that will give me border lines via the xml?If no, then will the
GridView
give me the lines I want?Will I be able to add all the perviously mentioned items I want to the
GridView
?
-
TronicZomB about 11 yearsI will look into the
GridLayout
combined with the background/ShapeDrawable you mentioned as a solution. Thank you for the info! -
ianthetechie over 9 yearsThanks for getting me thinking in the right direction. I was able to accomplish the "gridline" effect by using (in my case)
LinearLayout
s nested inside each other with differing background colors and margins.