Fix bug 5165223 Fix Holo Spinner item/dropdown alignment.

Fix bug 5117565 Spinner popup extends out of screen bounds.

Change-Id: I7fef7bbe98aa26611c498d1fe875e84db57dcda0
diff --git a/core/java/android/widget/ListPopupWindow.java b/core/java/android/widget/ListPopupWindow.java
index 307959b..a2910af 100644
--- a/core/java/android/widget/ListPopupWindow.java
+++ b/core/java/android/widget/ListPopupWindow.java
@@ -449,6 +449,8 @@
         if (popupBackground != null) {
             popupBackground.getPadding(mTempRect);
             mDropDownWidth = mTempRect.left + mTempRect.right + width;
+        } else {
+            setWidth(width);
         }
     }
 
diff --git a/core/java/android/widget/PopupWindow.java b/core/java/android/widget/PopupWindow.java
index 36927ca..4d45c2f 100644
--- a/core/java/android/widget/PopupWindow.java
+++ b/core/java/android/widget/PopupWindow.java
@@ -1065,9 +1065,10 @@
     private boolean findDropDownPosition(View anchor, WindowManager.LayoutParams p,
             int xoff, int yoff) {
 
+        final int anchorHeight = anchor.getHeight();
         anchor.getLocationInWindow(mDrawingLocation);
         p.x = mDrawingLocation[0] + xoff;
-        p.y = mDrawingLocation[1] + anchor.getHeight() + yoff;
+        p.y = mDrawingLocation[1] + anchorHeight + yoff;
         
         boolean onTop = false;
 
@@ -1076,9 +1077,12 @@
         anchor.getLocationOnScreen(mScreenLocation);
         final Rect displayFrame = new Rect();
         anchor.getWindowVisibleDisplayFrame(displayFrame);
+
+        int screenY = mScreenLocation[1] + anchorHeight + yoff;
         
         final View root = anchor.getRootView();
-        if (p.y + mPopupHeight > displayFrame.bottom || p.x + mPopupWidth - root.getWidth() > 0) {
+        if (screenY + mPopupHeight > displayFrame.bottom ||
+                p.x + mPopupWidth - root.getWidth() > 0) {
             // if the drop down disappears at the bottom of the screen. we try to
             // scroll a parent scrollview or move the drop down back up on top of
             // the edit box
diff --git a/core/java/android/widget/Spinner.java b/core/java/android/widget/Spinner.java
index 2fba18b..27d44bf 100644
--- a/core/java/android/widget/Spinner.java
+++ b/core/java/android/widget/Spinner.java
@@ -714,14 +714,27 @@
 
         @Override
         public void show() {
+            final int spinnerPaddingLeft = Spinner.this.getPaddingLeft();
             if (mDropDownWidth == WRAP_CONTENT) {
-                setWidth(Math.max(measureContentWidth((SpinnerAdapter) mAdapter, getBackground()),
-                        Spinner.this.getWidth()));
+                final int spinnerWidth = Spinner.this.getWidth();
+                final int spinnerPaddingRight = Spinner.this.getPaddingRight();
+                setContentWidth(Math.max(
+                        measureContentWidth((SpinnerAdapter) mAdapter, getBackground()),
+                        spinnerWidth - spinnerPaddingLeft - spinnerPaddingRight));
             } else if (mDropDownWidth == MATCH_PARENT) {
-                setWidth(Spinner.this.getWidth());
+                final int spinnerWidth = Spinner.this.getWidth();
+                final int spinnerPaddingRight = Spinner.this.getPaddingRight();
+                setContentWidth(spinnerWidth - spinnerPaddingLeft - spinnerPaddingRight);
             } else {
-                setWidth(mDropDownWidth);
+                setContentWidth(mDropDownWidth);
             }
+            final Drawable background = getBackground();
+            int bgOffset = 0;
+            if (background != null) {
+                background.getPadding(mTempRect);
+                bgOffset = -mTempRect.left;
+            }
+            setHorizontalOffset(bgOffset + spinnerPaddingLeft);
             setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
             super.show();
             getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
diff --git a/core/res/res/values/styles.xml b/core/res/res/values/styles.xml
index d20d16c..0f7bb17 100644
--- a/core/res/res/values/styles.xml
+++ b/core/res/res/values/styles.xml
@@ -1772,6 +1772,8 @@
 
     <style name="Widget.Holo.DropDownItem" parent="Widget.DropDownItem">
         <item name="android:textAppearance">@style/TextAppearance.Holo.Widget.DropDownItem</item>
+        <item name="android:paddingLeft">8dp</item>
+        <item name="android:paddingRight">8dp</item>
     </style>
 
     <style name="Widget.Holo.DropDownItem.Spinner">
@@ -1779,6 +1781,8 @@
 
     <style name="Widget.Holo.TextView.SpinnerItem" parent="Widget.TextView.SpinnerItem">
         <item name="android:textAppearance">@style/TextAppearance.Holo.Widget.TextView.SpinnerItem</item>
+        <item name="android:paddingLeft">8dp</item>
+        <item name="android:paddingRight">8dp</item>
     </style>
 
     <style name="Widget.Holo.KeyboardView" parent="Widget.KeyboardView">