10

I have created gridview with customer adapter. To give each cell border, I have placed them in two layouts. The first layout has black bg and second layout has white bg and contents. and I have given the parent layout 1dp padding, which given a border look

but the problem is that when two cells meet vertically, their border size becomes 2dp i.e. one cell's bottom border merges into other cell's top border.

But I like to create border as in given image

border example

Here is code of my current cell's xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layBorder"
    android:layout_width="77dp"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:orientation="vertical"
    android:padding="1dp" >

    <FrameLayout
        android:id="@+id/FrameLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF" >

        <ImageView
            android:id="@+id/ivElementName"
            android:layout_width="40dp"
            android:layout_height="60dp"
            android:layout_gravity="center_vertical|center_horizontal"
            android:layout_margin="1dp"
            android:adjustViewBounds="true"
            android:background="#00000000"
            android:maxHeight="60dp"
            android:maxWidth="40dp"
            android:minHeight="60dp"
            android:minWidth="40dp" />

        <ImageView
            android:id="@+id/ivElementImg"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_gravity="bottom|center_vertical|center_horizontal"
            android:layout_marginLeft="1dp"
            android:layout_marginRight="1dp"
            android:adjustViewBounds="true"
            android:background="#00000000"
            android:baselineAlignBottom="true"
            android:cropToPadding="true"
            android:maxHeight="30dp"
            android:maxWidth="30dp"
            android:minHeight="30dp"
            android:minWidth="30dp"
            android:visibility="invisible" />

    </FrameLayout>

</LinearLayout>
Adil Bhatty
  • 17,190
  • 34
  • 81
  • 118
  • I am not sure but what you want to achive can be done with Tablelayout. – Sushil Aug 19 '13 at 08:32
  • @Sushil, in table layout I will be having problem in populating the cells, and even in table layout the cells border will double where cells will meet. – Adil Bhatty Aug 19 '13 at 08:46

1 Answers1

26

You should do the next:

  • set background color of your gridview it will be a border color
  • set background color of your grid item as you need
  • set vertical and horizontal spacing it will be a border thickness

And don't forget to change you grid item layout height as match_parent

GridView gv = findViewById(R.id.my_grid_view);
gv.setBackgroundColor(Color.WHITE);
gv.setVerticalSpacing(1);
gv.setHorizontalSpacing(1);
cooperok
  • 4,249
  • 3
  • 30
  • 48
  • 6
    This shows an empty element with background color when number of columns in GridView is 2 with odd number of elements. – Parth mehta Apr 08 '15 at 04:03