Include padding in calculation of column count.

Also rename background color and don't sepcify background in grid items.

Change-Id: I4b565604156864384626e75f0bfd41c98623f90a
diff --git a/packages/DocumentsUI/res/layout/fragment_directory.xml b/packages/DocumentsUI/res/layout/fragment_directory.xml
index 910a702..7aff497 100644
--- a/packages/DocumentsUI/res/layout/fragment_directory.xml
+++ b/packages/DocumentsUI/res/layout/fragment_directory.xml
@@ -46,6 +46,6 @@
         android:clipToPadding="false"
         android:scrollbarStyle="outsideOverlay"
         android:drawSelectorOnTop="true"
-        android:background="@color/item_doc_grid_background" />
+        android:background="@color/directory_background" />
 
 </com.android.documentsui.DirectoryView>
diff --git a/packages/DocumentsUI/res/layout/item_doc_grid.xml b/packages/DocumentsUI/res/layout/item_doc_grid.xml
index 5d8a834..bf39194 100644
--- a/packages/DocumentsUI/res/layout/item_doc_grid.xml
+++ b/packages/DocumentsUI/res/layout/item_doc_grid.xml
@@ -17,7 +17,6 @@
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="@dimen/grid_item_height"
-    android:background="@color/item_doc_grid_background"
     android:padding="@dimen/grid_item_padding">
 
     <ImageView
diff --git a/packages/DocumentsUI/res/values-night/colors.xml b/packages/DocumentsUI/res/values-night/colors.xml
index 87cac78..f77017d 100644
--- a/packages/DocumentsUI/res/values-night/colors.xml
+++ b/packages/DocumentsUI/res/values-night/colors.xml
@@ -15,6 +15,6 @@
 -->
 
 <resources>
-    <color name="item_doc_grid_background">#ff111111</color>
+    <color name="directory_background">#ff111111</color>
     <color name="item_doc_grid_protect_background">#88ffffff</color>
 </resources>
diff --git a/packages/DocumentsUI/res/values/colors.xml b/packages/DocumentsUI/res/values/colors.xml
index ba8d69f..6f57a2c 100644
--- a/packages/DocumentsUI/res/values/colors.xml
+++ b/packages/DocumentsUI/res/values/colors.xml
@@ -18,6 +18,6 @@
     <color name="material_grey_50">#fffafafa</color>
     <color name="material_grey_300">#ffeeeeee</color>
 
-    <color name="item_doc_grid_background">@color/material_grey_300</color>
+    <color name="directory_background">@color/material_grey_300</color>
     <color name="item_doc_grid_protect_background">#88000000</color>
 </resources>
diff --git a/packages/DocumentsUI/src/com/android/documentsui/DirectoryFragment.java b/packages/DocumentsUI/src/com/android/documentsui/DirectoryFragment.java
index 37e267c..3489aa4 100644
--- a/packages/DocumentsUI/src/com/android/documentsui/DirectoryFragment.java
+++ b/packages/DocumentsUI/src/com/android/documentsui/DirectoryFragment.java
@@ -510,8 +510,12 @@
             case MODE_GRID:
                 thumbSize = getResources().getDimensionPixelSize(R.dimen.grid_width);
                 if (mGridLayout == null) {
-                    int columnCount = Math.max(1, mRecView.getWidth() / thumbSize);
-                    mGridLayout = new GridLayoutManager(getContext(), columnCount);
+                    int itemPadding =
+                            getResources().getDimensionPixelSize(R.dimen.grid_item_padding);
+                    int viewPadding = mRecView.getPaddingLeft() + mRecView.getPaddingRight();
+                    int columnCount =
+                            (mRecView.getWidth() - viewPadding) / (thumbSize + itemPadding);
+                    mGridLayout = new GridLayoutManager(getContext(), Math.max(1, columnCount));
                 }
                 layout = mGridLayout;
                 break;