[Android] GridView: adjust the side padding dynamically

Ludovic Fauvet git at videolan.org
Tue Oct 9 16:28:09 CEST 2012


vlc-ports/android | branch: master | Ludovic Fauvet <etix at videolan.org> | Tue Oct  9 15:26:47 2012 +0200| [18053026c5c6c198aaee4ab6779373f80d88723f] | committer: Ludovic Fauvet

GridView: adjust the side padding dynamically

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=18053026c5c6c198aaee4ab6779373f80d88723f
---

 vlc-android/res/layout/video_grid.xml              |    2 --
 .../videolan/vlc/gui/video/VideoGridFragment.java  |   20 +++++++++++++-------
 2 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/vlc-android/res/layout/video_grid.xml b/vlc-android/res/layout/video_grid.xml
index b914550..26b4895 100644
--- a/vlc-android/res/layout/video_grid.xml
+++ b/vlc-android/res/layout/video_grid.xml
@@ -8,8 +8,6 @@
         android:id="@id/android:list"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
-        android:paddingLeft="20dip"
-        android:paddingRight="20dip"
         android:paddingTop="10dip"
         android:paddingBottom="10dip"
         android:clipToPadding="false"
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
index 8c06d87..eecc72e 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
@@ -163,14 +163,11 @@ public class VideoGridFragment extends SherlockGridFragment implements ISortable
         mVideoAdapter.clear();
     }
 
-    private boolean hasSpaceForGrid() {
+    private boolean hasSpaceForGrid(View v) {
         final Activity activity = getActivity();
         if (activity == null)
             return true;
 
-        final LayoutInflater inflater = (LayoutInflater)activity.getSystemService
-                (Context.LAYOUT_INFLATER_SERVICE);
-        final View v = inflater.inflate(R.layout.video_grid, null);
         final GridView grid = (GridView)v.findViewById(android.R.id.list);
 
         DisplayMetrics outMetrics = new DisplayMetrics();
@@ -185,13 +182,22 @@ public class VideoGridFragment extends SherlockGridFragment implements ISortable
     }
 
     private void updateViewMode() {
-        if (getView() == null) {
+        if (getView() == null || getActivity() == null) {
             Log.w(TAG, "Unable to setup the view");
             return;
         }
 
-        final GridView gv = (GridView)getView().findViewById(android.R.id.list);
-        if (hasSpaceForGrid()) {
+        GridView gv = (GridView)getView().findViewById(android.R.id.list);
+
+        // Compute the left/right padding dynamically
+        DisplayMetrics outMetrics = new DisplayMetrics();
+        getActivity().getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
+        int sidePadding = (int) (outMetrics.widthPixels / 100 * Math.pow(outMetrics.density, 3) / 2);
+        sidePadding = Math.max(0, Math.min(100, sidePadding));
+        gv.setPadding(sidePadding, gv.getPaddingTop(), sidePadding, gv.getPaddingBottom());
+
+        // Select between grid or list
+        if (hasSpaceForGrid(getView())) {
             Log.d(TAG, "Switching to grid mode");
             gv.setNumColumns(GridView.AUTO_FIT);
             gv.setStretchMode(GRID_STRETCH_MODE);



More information about the Android mailing list