From fe4f03cd0ca38e446f7690e1b5e076e65530bb37 Mon Sep 17 00:00:00 2001 From: Wenyi Wang Date: Sun, 14 Feb 2016 14:39:36 -0800 Subject: Set number of columns of photo picker dynamically We use magic number 135dp to get the number of columns. Here's the number of columns on Nexus devices: https://goto.google.com/devicecolumns Bug 27181878 Change-Id: I346caf488423586655cdd2a650c0502f7ff1bb9b --- res/layout/all_photos_button.xml | 4 ++-- res/layout/compact_photo_selection_fragment.xml | 2 +- res/layout/compact_photo_selection_item.xml | 4 ++-- res/layout/take_a_photo_button.xml | 4 ++-- res/values/dimens.xml | 4 ++-- .../editor/CompactPhotoSelectionFragment.java | 22 ++++++---------------- 6 files changed, 15 insertions(+), 25 deletions(-) diff --git a/res/layout/all_photos_button.xml b/res/layout/all_photos_button.xml index 6ccdca53d..83578c3a1 100644 --- a/res/layout/all_photos_button.xml +++ b/res/layout/all_photos_button.xml @@ -1,7 +1,7 @@ diff --git a/res/layout/compact_photo_selection_fragment.xml b/res/layout/compact_photo_selection_fragment.xml index b35dae5b0..42f5b963a 100644 --- a/res/layout/compact_photo_selection_fragment.xml +++ b/res/layout/compact_photo_selection_fragment.xml @@ -22,7 +22,7 @@ android:numColumns="auto_fit" android:verticalSpacing="3dp" android:horizontalSpacing="3dp" - android:stretchMode="none" + android:stretchMode="columnWidth" android:gravity="center" android:paddingTop="3dp" android:drawSelectorOnTop="true"/> diff --git a/res/layout/compact_photo_selection_item.xml b/res/layout/compact_photo_selection_item.xml index c625765b7..aea8ff611 100644 --- a/res/layout/compact_photo_selection_item.xml +++ b/res/layout/compact_photo_selection_item.xml @@ -21,8 +21,8 @@ diff --git a/res/layout/take_a_photo_button.xml b/res/layout/take_a_photo_button.xml index 28ec60bd4..b837e850c 100644 --- a/res/layout/take_a_photo_button.xml +++ b/res/layout/take_a_photo_button.xml @@ -1,7 +1,7 @@ diff --git a/res/values/dimens.xml b/res/values/dimens.xml index 8fe5c72b5..7b4cd12d8 100644 --- a/res/values/dimens.xml +++ b/res/values/dimens.xml @@ -280,8 +280,8 @@ of ListView's will not match the padding inside list items --> 12dp - - 1dp + + 135dp 8dp diff --git a/src/com/android/contacts/editor/CompactPhotoSelectionFragment.java b/src/com/android/contacts/editor/CompactPhotoSelectionFragment.java index bc9435b57..3f157a47c 100644 --- a/src/com/android/contacts/editor/CompactPhotoSelectionFragment.java +++ b/src/com/android/contacts/editor/CompactPhotoSelectionFragment.java @@ -28,7 +28,6 @@ import android.os.Bundle; import android.os.Parcel; import android.os.Parcelable; import android.provider.ContactsContract; -import android.text.TextUtils; import android.util.DisplayMetrics; import android.view.Display; import android.view.LayoutInflater; @@ -53,9 +52,6 @@ public class CompactPhotoSelectionFragment extends Fragment { private final int VIEW_TYPE_TAKE_PHOTO = 0; private final int VIEW_TYPE_ALL_PHOTOS = 1; private final int VIEW_TYPE_IMAGE = 2; - private final int NUMBER_OF_COLUMNS_PORTRAIT = 3; - private final int NUMBER_OF_COLUMNS_LANDSCAPE = 5; - private int mNumberOfColumns; /** * Callbacks hosts this Fragment. @@ -295,7 +291,7 @@ public class CompactPhotoSelectionFragment extends Fragment { public void onItemClick(AdapterView parent, View view, int position, long id) { final PhotoSourceDialogFragment.Listener listener = (PhotoSourceDialogFragment.Listener) getActivity(); - if (position == 0){ + if (position == 0) { listener.onTakePhotoChosen(); } else if (position == 1) { listener.onPickFromGalleryChosen(); @@ -312,17 +308,11 @@ public class CompactPhotoSelectionFragment extends Fragment { final Display display = getActivity().getWindowManager().getDefaultDisplay(); final DisplayMetrics outMetrics = new DisplayMetrics (); - display.getMetrics(outMetrics); - - // portrait -- 3 columns; landscape -- 5 columns. - mNumberOfColumns = outMetrics.heightPixels > outMetrics.widthPixels ? - NUMBER_OF_COLUMNS_PORTRAIT : NUMBER_OF_COLUMNS_LANDSCAPE; - final int paddingWidth = (int) getResources().getDimension(R.dimen - .photo_picker_column_padding_width); - float density = getResources().getDisplayMetrics().density; - float dpColumnWidth = (outMetrics.widthPixels - paddingWidth * (mNumberOfColumns - 1) * - density) / mNumberOfColumns; - mGridView.setColumnWidth((int) dpColumnWidth); + display.getRealMetrics(outMetrics); // real metrics include the navigation Bar + + final float numColumns = outMetrics.widthPixels / + getResources().getDimension(R.dimen.photo_picker_item_ideal_width); + mGridView.setNumColumns(Math.round(numColumns)); return view; } -- cgit v1.2.3