Skip to content

Commit

Permalink
fix dropdown not displaying correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench committed Dec 10, 2016
1 parent 950a2ae commit 561461c
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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);
Expand All @@ -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()));

Expand Down
133 changes: 83 additions & 50 deletions sample/src/main/res/layout/example_bootstrap_dropdown.xml
Original file line number Diff line number Diff line change
@@ -1,161 +1,194 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">
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"
>

<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:scrollbars="none">
android:scrollbars="none"
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:orientation="horizontal"
>

<com.beardedhen.androidbootstrap.BootstrapDropDown
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:bootstrapText="XSmall {fa_thumbs_o_up}"
app:roundedCorners="true"
app:bootstrapSize="xs"
app:bootstrapBrand="success"
app:bootstrapExpandDirection="down"
app:bootstrapSize="xs"
app:bootstrapText="XSmall {fa_thumbs_o_up}"
app:dropdownResource="@array/bootstrap_dropdown_example_data"
app:bootstrapExpandDirection="down" />
app:roundedCorners="true"
/>

<com.beardedhen.androidbootstrap.BootstrapDropDown
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
app:bootstrapText="Small {fa_thumbs_o_up}"
app:bootstrapBrand="danger"
app:bootstrapExpandDirection="down"
app:bootstrapSize="sm"
app:roundedCorners="true"
app:bootstrapText="Small {fa_thumbs_o_up}"
app:dropdownResource="@array/bootstrap_dropdown_example_data"
app:bootstrapExpandDirection="down"/>
app:roundedCorners="true"
/>

<com.beardedhen.androidbootstrap.BootstrapDropDown
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
app:bootstrapText="Medium {fa_thumbs_o_up}"
app:bootstrapBrand="regular"
app:roundedCorners="true"
app:bootstrapExpandDirection="down"
app:bootstrapSize="md"
app:bootstrapText="Medium {fa_thumbs_o_up}"
app:dropdownResource="@array/bootstrap_dropdown_example_data"
app:bootstrapExpandDirection="down"/>
app:roundedCorners="true"
/>

<com.beardedhen.androidbootstrap.BootstrapDropDown
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
app:bootstrapText="Large {fa_thumbs_o_up}"
app:bootstrapBrand="secondary"
app:roundedCorners="true"
app:bootstrapExpandDirection="down"
app:bootstrapSize="lg"
app:bootstrapText="Large {fa_thumbs_o_up}"
app:dropdownResource="@array/bootstrap_dropdown_example_data"
app:bootstrapExpandDirection="down"/>
app:roundedCorners="true"
/>

<com.beardedhen.androidbootstrap.BootstrapDropDown
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
app:bootstrapText="XLarge {fa_thumbs_o_up}"
app:bootstrapBrand="warning"
app:roundedCorners="true"
app:bootstrapExpandDirection="down"
app:bootstrapSize="xl"
app:bootstrapText="XLarge {fa_thumbs_o_up}"
app:dropdownResource="@array/bootstrap_dropdown_example_data"
app:bootstrapExpandDirection="down"/>
app:roundedCorners="true"
/>
</LinearLayout>
</HorizontalScrollView>

<com.beardedhen.androidbootstrap.BootstrapDropDown
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
app:bootstrapExpandDirection="down"
app:bootstrapText="Left"
android:gravity="start"
app:dropdownResource="@array/bootstrap_dropdown_example_data"
app:bootstrapExpandDirection="down" />
/>

<com.beardedhen.androidbootstrap.BootstrapDropDown
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
app:bootstrapExpandDirection="down"
app:bootstrapText="Center"
app:dropdownResource="@array/bootstrap_dropdown_example_data"
app:bootstrapExpandDirection="down" />
/>

<com.beardedhen.androidbootstrap.BootstrapDropDown
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:bootstrapText="Right"
app:roundedCorners="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
app:bootstrapExpandDirection="down"
app:bootstrapText="Right"
android:gravity="end"
app:dropdownResource="@array/bootstrap_dropdown_example_data"
app:bootstrapExpandDirection="down" />
app:roundedCorners="true"
/>

<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:scrollbars="none">
android:scrollbars="none"
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:orientation="horizontal"
>

<com.beardedhen.androidbootstrap.BootstrapDropDown
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:bootstrapBrand="success"
app:bootstrapExpandDirection="up"
app:bootstrapSize="xs"
app:bootstrapText="XSmall {fa_thumbs_o_up}"
app:dropdownResource="@array/bootstrap_dropdown_example_data"
app:roundedCorners="true"
app:showOutline="true"
app:bootstrapSize="xs"
app:bootstrapBrand="success"
app:dropdownResource="@array/bootstrap_dropdown_example_data"
app:bootstrapExpandDirection="up" />
/>

<com.beardedhen.androidbootstrap.BootstrapDropDown
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
app:bootstrapText="Small {fa_thumbs_o_up}"
app:bootstrapBrand="danger"
app:showOutline="true"
app:bootstrapExpandDirection="up"
app:bootstrapSize="sm"
app:roundedCorners="true"
app:bootstrapText="Small {fa_thumbs_o_up}"
app:dropdownResource="@array/bootstrap_dropdown_example_data"
app:bootstrapExpandDirection="up"/>
app:roundedCorners="true"
app:showOutline="true"
/>

<com.beardedhen.androidbootstrap.BootstrapDropDown
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
app:bootstrapText="Medium {fa_thumbs_o_up}"
app:bootstrapBrand="regular"
app:roundedCorners="true"
app:showOutline="true"
app:bootstrapExpandDirection="up"
app:bootstrapSize="md"
app:bootstrapText="Medium {fa_thumbs_o_up}"
app:dropdownResource="@array/bootstrap_dropdown_example_data"
app:bootstrapExpandDirection="up"/>
app:roundedCorners="true"
app:showOutline="true"
/>

<com.beardedhen.androidbootstrap.BootstrapDropDown
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
app:bootstrapText="Large {fa_thumbs_o_up}"
app:bootstrapBrand="secondary"
app:roundedCorners="true"
app:showOutline="true"
app:bootstrapExpandDirection="up"
app:bootstrapSize="lg"
app:bootstrapText="Large {fa_thumbs_o_up}"
app:dropdownResource="@array/bootstrap_dropdown_example_data"
app:bootstrapExpandDirection="up"/>
app:roundedCorners="true"
app:showOutline="true"
/>

<com.beardedhen.androidbootstrap.BootstrapDropDown
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
app:bootstrapText="XLarge {fa_thumbs_o_up}"
app:bootstrapBrand="warning"
app:showOutline="true"
app:roundedCorners="true"
app:bootstrapExpandDirection="up"
app:bootstrapSize="xl"
app:bootstrapText="XLarge {fa_thumbs_o_up}"
app:dropdownResource="@array/bootstrap_dropdown_example_data"
app:bootstrapExpandDirection="up"/>
app:roundedCorners="true"
app:showOutline="true"
/>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
12 changes: 12 additions & 0 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,17 @@
<item>Fifth item</item>
<item>{dropdown_separator}</item>
<item>Separated item</item>
<item>Item 9</item>
<item>Item 10</item>
<item>Item 11</item>
<item>Item 12</item>
<item>Item 13</item>
<item>Item 14</item>
<item>Item 15</item>
<item>Item 16</item>
<item>Item 17</item>
<item>Item 18</item>
<item>Item 19</item>
<item>Item 20</item>
</array>
</resources>

0 comments on commit 561461c

Please sign in to comment.