Bug 1137483 - Center SearchEngineBar when it doesn't fill the screen. r=liuche
☠☠ backed out by c53c5bd43297 ☠ ☠
authorMichael Comella <michael.l.comella@gmail.com>
Tue, 21 Apr 2015 17:12:43 -0700
changeset 272489 399980ce108a47e41be77480eacf731a39e91ada
parent 272488 0602fff3681db69f5dc2d6ef7745f4b8fabfa5f6
child 272490 a7ba18a83520fe460148f766b8cc38f0252bb908
push id4830
push userjlund@mozilla.com
push dateMon, 29 Jun 2015 20:18:48 +0000
treeherdermozilla-beta@4c2175bb0420 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersliuche
bugs1137483
milestone40.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1137483 - Center SearchEngineBar when it doesn't fill the screen. r=liuche
mobile/android/base/home/SearchEngineBar.java
mobile/android/base/resources/layout/browser_search.xml
mobile/android/base/resources/layout/search_engine_bar_item.xml
--- a/mobile/android/base/home/SearchEngineBar.java
+++ b/mobile/android/base/home/SearchEngineBar.java
@@ -9,16 +9,17 @@ import android.content.Context;
 import android.graphics.Canvas;
 import android.graphics.Paint;
 import android.util.AttributeSet;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.AdapterView;
 import android.widget.BaseAdapter;
+import android.widget.FrameLayout;
 
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.widget.FaviconView;
 import org.mozilla.gecko.widget.TwoWayView;
 
 import java.util.ArrayList;
 import java.util.List;
 
@@ -28,34 +29,22 @@ public class SearchEngineBar extends Two
 
     public interface OnSearchBarClickListener {
         public void onSearchBarClickListener(SearchEngine searchEngine);
     }
 
     private final SearchEngineAdapter adapter;
     private OnSearchBarClickListener onSearchBarClickListener;
 
-    private final Paint dividerPaint;
-
     public SearchEngineBar(final Context context, final AttributeSet attrs) {
         super(context, attrs);
 
         adapter = new SearchEngineAdapter();
         setAdapter(adapter);
         setOnItemClickListener(this);
-
-        dividerPaint = new Paint();
-        dividerPaint.setColor(getResources().getColor(R.color.divider_light));
-    }
-
-    @Override
-    public void onDraw(final Canvas canvas) {
-        super.onDraw(canvas);
-
-        canvas.drawLine(0, 0, getWidth(), 0, dividerPaint);
     }
 
     @Override
     public void onItemClick(final AdapterView<?> parent, final View view, final int position,
             final long id) {
         if (onSearchBarClickListener == null) {
             throw new IllegalStateException(
                     OnSearchBarClickListener.class.getSimpleName() + " is not initialized");
@@ -107,9 +96,38 @@ public class SearchEngineBar extends Two
 
             final FaviconView faviconView = (FaviconView) view.findViewById(R.id.search_engine_icon);
             final SearchEngine searchEngine = searchEngines.get(position);
             faviconView.updateAndScaleImage(searchEngine.getIcon(), searchEngine.getEngineIdentifier());
 
             return view;
         }
     }
+
+    /**
+     * A Container to surround the SearchEngineBar. This is necessary so we can draw
+     * a divider across the entire width of the screen, but have the inner list layout
+     * not take up the full width of the screen so it can be centered within this container
+     * if there aren't enough items that it needs to scroll.
+     *
+     * Note: a better implementation would have this View inflating an inner layout so
+     * the containing layout doesn't need two "SearchEngineBar" Views but it wasn't
+     * worth the refactor time.
+     */
+    @SuppressWarnings("unused") // via XML
+    public static class SearchEngineBarContainer extends FrameLayout {
+        private final Paint dividerPaint;
+
+        public SearchEngineBarContainer(final Context context, final AttributeSet attrs) {
+            super(context, attrs);
+
+            dividerPaint = new Paint();
+            dividerPaint.setColor(getResources().getColor(R.color.divider_light));
+        }
+
+        @Override
+        public void onDraw(final Canvas canvas) {
+            super.onDraw(canvas);
+
+            canvas.drawLine(0, 0, getWidth(), 0, dividerPaint);
+        }
+    }
 }
--- a/mobile/android/base/resources/layout/browser_search.xml
+++ b/mobile/android/base/resources/layout/browser_search.xml
@@ -14,29 +14,31 @@
               android:layout="@layout/home_suggestion_prompt" />
 
     <view class="org.mozilla.gecko.home.BrowserSearch$HomeSearchListView"
             android:id="@+id/home_list_view"
             android:layout_width="match_parent"
             android:layout_height="0dp"
             android:layout_weight="1" />
 
-    <!-- The desired layout_height is 48dp. We add paddingTop so we have
-         space to dynamically draw the divider in onDraw. Preferably, we'd
-         wrap_content on the inner layout, but TwoWayView ignores wrap_content. :(
+    <view class="org.mozilla.gecko.home.SearchEngineBar$SearchEngineBarContainer"
+          android:layout_width="match_parent"
+          android:layout_height="wrap_content"
+          android:background="#fff">
 
-         Note: the layout_height value is shared with the inner layout
-         (search_engine_bar_item at the time of this writing).
+        <!-- We add a marginTop so the outer container can draw a divider.
 
-         listSelector is too slow for showing pressed state
-         so we set the pressed colors on the child. -->
-    <org.mozilla.gecko.home.SearchEngineBar
-          android:id="@+id/search_engine_bar"
-          android:layout_width="match_parent"
-          android:layout_height="49dp"
-          android:paddingTop="1dp"
-          android:orientation="horizontal"
-          android:background="#fff"
-          android:choiceMode="singleChoice"
-          android:listSelector="@android:color/transparent"
-          android:cacheColorHint="@android:color/transparent"/>
+             listSelector is too slow for showing pressed state
+             so we set the pressed colors on the child. -->
+        <org.mozilla.gecko.home.SearchEngineBar
+              android:id="@+id/search_engine_bar"
+              android:layout_width="wrap_content"
+              android:layout_height="48dp"
+              android:layout_marginTop="1dp"
+              android:orientation="horizontal"
+              android:layout_gravity="center_horizontal"
+              android:choiceMode="singleChoice"
+              android:listSelector="@android:color/transparent"
+              android:cacheColorHint="@android:color/transparent"/>
+
+   </view>
 
 </LinearLayout>
--- a/mobile/android/base/resources/layout/search_engine_bar_item.xml
+++ b/mobile/android/base/resources/layout/search_engine_bar_item.xml
@@ -6,17 +6,17 @@
 <!-- TwoWayView doesn't let us set the margin around items (except as
      gecko:itemMargin, but that doesn't increase the hit area) so we
      have to surround the main View by a ViewGroup to create a pressable margin.
 
      Note: the layout_height values are shared with the parent
      View (browser_search at the time of this writing). -->
 <FrameLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_height="48dp"
+    android:layout_height="match_parent"
     android:layout_width="72dp"
     android:background="@color/pressed_about_page_header_grey">
 
     <!-- Width & height are set to make the Favicons as sharp as possible
          based on asset size. -->
     <org.mozilla.gecko.widget.FaviconView
         android:id="@+id/search_engine_icon"
         android:layout_width="16dp"