diff --git a/AndroidBootstrap/src/main/java/com/beardedhen/androidbootstrap/BootstrapDropDown.java b/AndroidBootstrap/src/main/java/com/beardedhen/androidbootstrap/BootstrapDropDown.java index 303167d..33ce2c6 100644 --- a/AndroidBootstrap/src/main/java/com/beardedhen/androidbootstrap/BootstrapDropDown.java +++ b/AndroidBootstrap/src/main/java/com/beardedhen/androidbootstrap/BootstrapDropDown.java @@ -51,6 +51,7 @@ public class BootstrapDropDown extends AwesomeTextView implements View.OnClickLi private static final String REPLACE_REGEX_HEADER = "\\{dropdown_header\\}"; private static final String REPLACE_REGEX_SEPARATOR = "\\{dropdown_separator\\}"; private static final String REPLACE_REGEX_DISABLED = "\\{dropdown_disabled\\}"; + private static final int SCREEN_WIDTH_GUESS = 1000; private ExpandDirection expandDirection; private PopupWindow dropdownWindow; @@ -103,9 +104,16 @@ private void initialise(AttributeSet attrs) { int sizeOrdinal = a.getInt(R.styleable.BootstrapDropDown_bootstrapSize, -1); expandDirection = ExpandDirection.fromAttributeValue(directionOrdinal); - dropdownData = getContext().getResources().getStringArray(dataOrdinal); + bootstrapSize = DefaultBootstrapSize.fromAttributeValue(sizeOrdinal).scaleFactor(); itemHeight = a.getDimensionPixelSize(R.styleable.BootstrapDropDown_itemHeight, (int) DimenUtils.pixelsFromDpResource(getContext(), R.dimen.bootstrap_dropdown_default_item_height)); + + if (isInEditMode()) { + dropdownData = new String[] {"Android Studio", "Layout Preview", "Is Always", "Breaking"}; + } + else { + dropdownData = getContext().getResources().getStringArray(dataOrdinal); + } } finally { a.recycle(); @@ -120,9 +128,14 @@ private void initialise(AttributeSet attrs) { baselineVertPadding = DimenUtils.pixelsFromDpResource(getContext(), R.dimen.bootstrap_button_default_vert_padding); baselineHoriPadding = DimenUtils.pixelsFromDpResource(getContext(), R.dimen.bootstrap_button_default_hori_padding); - DisplayMetrics metrics = new DisplayMetrics(); - ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics); - screenWidth = metrics.widthPixels; + if (isInEditMode()) { + screenWidth = SCREEN_WIDTH_GUESS; // take a sensible guess + } + else { + DisplayMetrics metrics = new DisplayMetrics(); + ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics); + screenWidth = metrics.widthPixels; + } createDropDown(); updateDropDownState(); @@ -133,8 +146,12 @@ private void createDropDown() { dropdownWindow = new PopupWindow(); dropdownWindow.setFocusable(true); dropdownWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT); - dropdownWindow.setBackgroundDrawable(DrawableUtils.resolveDrawable(android.R.drawable - .dialog_holo_light_frame, getContext())); + + if (!isInEditMode()) { + dropdownWindow.setBackgroundDrawable(DrawableUtils.resolveDrawable(android.R.drawable + .dialog_holo_light_frame, getContext())); + } + dropdownWindow.setContentView(dropdownView); dropdownWindow.setOnDismissListener(this); dropdownWindow.setAnimationStyle(android.R.style.Animation_Activity); @@ -155,14 +172,16 @@ private ScrollView createDropDownView() { int clickableChildCounter = 0; dropdownView.setOrientation(LinearLayout.VERTICAL); - LayoutParams childParams = new LayoutParams(LayoutParams.MATCH_PARENT, DimenUtils.pixelsToDp(itemHeight * bootstrapSize)); + int height = (int) (itemHeight * bootstrapSize); + LayoutParams childParams = new LayoutParams(LayoutParams.MATCH_PARENT, height); for (String text : dropdownData) { TextView childView = new TextView(getContext()); childView.setGravity(Gravity.CENTER_VERTICAL); childView.setLayoutParams(childParams); - childView.setPadding(DimenUtils.dpToPixels(baselineItemLeftPadding * bootstrapSize), 0, - DimenUtils.dpToPixels(baselineItemRightPadding * bootstrapSize), 0); + + int padding = (int) (baselineItemLeftPadding * bootstrapSize); + childView.setPadding(padding, 0, padding, 0); childView.setTextSize(baselineDropDownViewFontSize * bootstrapSize); childView.setTextColor(ColorUtils.resolveColor(android.R.color.black, getContext())); diff --git a/sample/src/main/res/layout/example_bootstrap_dropdown.xml b/sample/src/main/res/layout/example_bootstrap_dropdown.xml index a8ffaac..39535c8 100644 --- a/sample/src/main/res/layout/example_bootstrap_dropdown.xml +++ b/sample/src/main/res/layout/example_bootstrap_dropdown.xml @@ -1,161 +1,194 @@ + xmlns:app="http://schemas.android.com/apk/res-auto" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:orientation="vertical" + android:padding="8dp" + > + + android:scrollbars="none" + > + + android:orientation="horizontal" + > + + app:roundedCorners="true" + /> + app:roundedCorners="true" + /> + app:roundedCorners="true" + /> + + app:roundedCorners="true" + /> + + app:roundedCorners="true" + /> + + /> + + /> + + app:roundedCorners="true" + /> + + android:scrollbars="none" + > + + android:orientation="horizontal" + > + + /> + app:roundedCorners="true" + app:showOutline="true" + /> + app:roundedCorners="true" + app:showOutline="true" + /> + + app:roundedCorners="true" + app:showOutline="true" + /> + + app:roundedCorners="true" + app:showOutline="true" + /> \ No newline at end of file diff --git a/sample/src/main/res/values/strings.xml b/sample/src/main/res/values/strings.xml index c74be9c..966dd42 100644 --- a/sample/src/main/res/values/strings.xml +++ b/sample/src/main/res/values/strings.xml @@ -9,5 +9,17 @@ Fifth item {dropdown_separator} Separated item + Item 9 + Item 10 + Item 11 + Item 12 + Item 13 + Item 14 + Item 15 + Item 16 + Item 17 + Item 18 + Item 19 + Item 20