Skip to content

Commit 561461c

Browse files
committed
fix dropdown not displaying correctly
1 parent 950a2ae commit 561461c

3 files changed

Lines changed: 123 additions & 59 deletions

File tree

AndroidBootstrap/src/main/java/com/beardedhen/androidbootstrap/BootstrapDropDown.java

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public class BootstrapDropDown extends AwesomeTextView implements View.OnClickLi
5151
private static final String REPLACE_REGEX_HEADER = "\\{dropdown_header\\}";
5252
private static final String REPLACE_REGEX_SEPARATOR = "\\{dropdown_separator\\}";
5353
private static final String REPLACE_REGEX_DISABLED = "\\{dropdown_disabled\\}";
54+
private static final int SCREEN_WIDTH_GUESS = 1000;
5455

5556
private ExpandDirection expandDirection;
5657
private PopupWindow dropdownWindow;
@@ -103,9 +104,16 @@ private void initialise(AttributeSet attrs) {
103104
int sizeOrdinal = a.getInt(R.styleable.BootstrapDropDown_bootstrapSize, -1);
104105

105106
expandDirection = ExpandDirection.fromAttributeValue(directionOrdinal);
106-
dropdownData = getContext().getResources().getStringArray(dataOrdinal);
107+
107108
bootstrapSize = DefaultBootstrapSize.fromAttributeValue(sizeOrdinal).scaleFactor();
108109
itemHeight = a.getDimensionPixelSize(R.styleable.BootstrapDropDown_itemHeight, (int) DimenUtils.pixelsFromDpResource(getContext(), R.dimen.bootstrap_dropdown_default_item_height));
110+
111+
if (isInEditMode()) {
112+
dropdownData = new String[] {"Android Studio", "Layout Preview", "Is Always", "Breaking"};
113+
}
114+
else {
115+
dropdownData = getContext().getResources().getStringArray(dataOrdinal);
116+
}
109117
}
110118
finally {
111119
a.recycle();
@@ -120,9 +128,14 @@ private void initialise(AttributeSet attrs) {
120128
baselineVertPadding = DimenUtils.pixelsFromDpResource(getContext(), R.dimen.bootstrap_button_default_vert_padding);
121129
baselineHoriPadding = DimenUtils.pixelsFromDpResource(getContext(), R.dimen.bootstrap_button_default_hori_padding);
122130

123-
DisplayMetrics metrics = new DisplayMetrics();
124-
((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics);
125-
screenWidth = metrics.widthPixels;
131+
if (isInEditMode()) {
132+
screenWidth = SCREEN_WIDTH_GUESS; // take a sensible guess
133+
}
134+
else {
135+
DisplayMetrics metrics = new DisplayMetrics();
136+
((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics);
137+
screenWidth = metrics.widthPixels;
138+
}
126139

127140
createDropDown();
128141
updateDropDownState();
@@ -133,8 +146,12 @@ private void createDropDown() {
133146
dropdownWindow = new PopupWindow();
134147
dropdownWindow.setFocusable(true);
135148
dropdownWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
136-
dropdownWindow.setBackgroundDrawable(DrawableUtils.resolveDrawable(android.R.drawable
137-
.dialog_holo_light_frame, getContext()));
149+
150+
if (!isInEditMode()) {
151+
dropdownWindow.setBackgroundDrawable(DrawableUtils.resolveDrawable(android.R.drawable
152+
.dialog_holo_light_frame, getContext()));
153+
}
154+
138155
dropdownWindow.setContentView(dropdownView);
139156
dropdownWindow.setOnDismissListener(this);
140157
dropdownWindow.setAnimationStyle(android.R.style.Animation_Activity);
@@ -155,14 +172,16 @@ private ScrollView createDropDownView() {
155172
int clickableChildCounter = 0;
156173

157174
dropdownView.setOrientation(LinearLayout.VERTICAL);
158-
LayoutParams childParams = new LayoutParams(LayoutParams.MATCH_PARENT, DimenUtils.pixelsToDp(itemHeight * bootstrapSize));
175+
int height = (int) (itemHeight * bootstrapSize);
176+
LayoutParams childParams = new LayoutParams(LayoutParams.MATCH_PARENT, height);
159177

160178
for (String text : dropdownData) {
161179
TextView childView = new TextView(getContext());
162180
childView.setGravity(Gravity.CENTER_VERTICAL);
163181
childView.setLayoutParams(childParams);
164-
childView.setPadding(DimenUtils.dpToPixels(baselineItemLeftPadding * bootstrapSize), 0,
165-
DimenUtils.dpToPixels(baselineItemRightPadding * bootstrapSize), 0);
182+
183+
int padding = (int) (baselineItemLeftPadding * bootstrapSize);
184+
childView.setPadding(padding, 0, padding, 0);
166185
childView.setTextSize(baselineDropDownViewFontSize * bootstrapSize);
167186
childView.setTextColor(ColorUtils.resolveColor(android.R.color.black, getContext()));
168187

Lines changed: 83 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,161 +1,194 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:app="http://schemas.android.com/apk/res-auto"
4-
android:orientation="vertical"
5-
android:layout_width="match_parent"
6-
android:layout_height="match_parent"
7-
android:padding="8dp">
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
android:orientation="vertical"
7+
android:padding="8dp"
8+
>
9+
810
<HorizontalScrollView
911
android:layout_width="match_parent"
1012
android:layout_height="wrap_content"
1113
android:layout_alignParentTop="true"
12-
android:scrollbars="none">
14+
android:scrollbars="none"
15+
>
16+
1317
<LinearLayout
1418
android:layout_width="match_parent"
1519
android:layout_height="wrap_content"
16-
android:orientation="horizontal">
20+
android:orientation="horizontal"
21+
>
22+
1723
<com.beardedhen.androidbootstrap.BootstrapDropDown
1824
android:layout_width="wrap_content"
1925
android:layout_height="wrap_content"
20-
app:bootstrapText="XSmall {fa_thumbs_o_up}"
21-
app:roundedCorners="true"
22-
app:bootstrapSize="xs"
2326
app:bootstrapBrand="success"
27+
app:bootstrapExpandDirection="down"
28+
app:bootstrapSize="xs"
29+
app:bootstrapText="XSmall {fa_thumbs_o_up}"
2430
app:dropdownResource="@array/bootstrap_dropdown_example_data"
25-
app:bootstrapExpandDirection="down" />
31+
app:roundedCorners="true"
32+
/>
2633

2734
<com.beardedhen.androidbootstrap.BootstrapDropDown
2835
android:layout_width="wrap_content"
2936
android:layout_height="wrap_content"
3037
android:layout_marginLeft="8dp"
31-
app:bootstrapText="Small {fa_thumbs_o_up}"
3238
app:bootstrapBrand="danger"
39+
app:bootstrapExpandDirection="down"
3340
app:bootstrapSize="sm"
34-
app:roundedCorners="true"
41+
app:bootstrapText="Small {fa_thumbs_o_up}"
3542
app:dropdownResource="@array/bootstrap_dropdown_example_data"
36-
app:bootstrapExpandDirection="down"/>
43+
app:roundedCorners="true"
44+
/>
3745

3846
<com.beardedhen.androidbootstrap.BootstrapDropDown
3947
android:layout_width="wrap_content"
4048
android:layout_height="wrap_content"
4149
android:layout_marginLeft="8dp"
42-
app:bootstrapText="Medium {fa_thumbs_o_up}"
4350
app:bootstrapBrand="regular"
44-
app:roundedCorners="true"
51+
app:bootstrapExpandDirection="down"
4552
app:bootstrapSize="md"
53+
app:bootstrapText="Medium {fa_thumbs_o_up}"
4654
app:dropdownResource="@array/bootstrap_dropdown_example_data"
47-
app:bootstrapExpandDirection="down"/>
55+
app:roundedCorners="true"
56+
/>
57+
4858
<com.beardedhen.androidbootstrap.BootstrapDropDown
4959
android:layout_width="wrap_content"
5060
android:layout_height="wrap_content"
5161
android:layout_marginLeft="8dp"
52-
app:bootstrapText="Large {fa_thumbs_o_up}"
5362
app:bootstrapBrand="secondary"
54-
app:roundedCorners="true"
63+
app:bootstrapExpandDirection="down"
5564
app:bootstrapSize="lg"
65+
app:bootstrapText="Large {fa_thumbs_o_up}"
5666
app:dropdownResource="@array/bootstrap_dropdown_example_data"
57-
app:bootstrapExpandDirection="down"/>
67+
app:roundedCorners="true"
68+
/>
69+
5870
<com.beardedhen.androidbootstrap.BootstrapDropDown
5971
android:layout_width="wrap_content"
6072
android:layout_height="wrap_content"
6173
android:layout_marginLeft="8dp"
62-
app:bootstrapText="XLarge {fa_thumbs_o_up}"
6374
app:bootstrapBrand="warning"
64-
app:roundedCorners="true"
75+
app:bootstrapExpandDirection="down"
6576
app:bootstrapSize="xl"
77+
app:bootstrapText="XLarge {fa_thumbs_o_up}"
6678
app:dropdownResource="@array/bootstrap_dropdown_example_data"
67-
app:bootstrapExpandDirection="down"/>
79+
app:roundedCorners="true"
80+
/>
6881
</LinearLayout>
6982
</HorizontalScrollView>
83+
7084
<com.beardedhen.androidbootstrap.BootstrapDropDown
7185
android:layout_width="wrap_content"
7286
android:layout_height="wrap_content"
7387
android:layout_alignParentLeft="true"
7488
android:layout_centerVertical="true"
89+
app:bootstrapExpandDirection="down"
7590
app:bootstrapText="Left"
91+
android:gravity="start"
7692
app:dropdownResource="@array/bootstrap_dropdown_example_data"
77-
app:bootstrapExpandDirection="down" />
93+
/>
94+
7895
<com.beardedhen.androidbootstrap.BootstrapDropDown
7996
android:layout_width="wrap_content"
8097
android:layout_height="wrap_content"
8198
android:layout_centerInParent="true"
99+
app:bootstrapExpandDirection="down"
82100
app:bootstrapText="Center"
83101
app:dropdownResource="@array/bootstrap_dropdown_example_data"
84-
app:bootstrapExpandDirection="down" />
102+
/>
103+
85104
<com.beardedhen.androidbootstrap.BootstrapDropDown
86105
android:layout_width="wrap_content"
87106
android:layout_height="wrap_content"
88-
app:bootstrapText="Right"
89-
app:roundedCorners="true"
90107
android:layout_alignParentRight="true"
91108
android:layout_centerVertical="true"
109+
app:bootstrapExpandDirection="down"
110+
app:bootstrapText="Right"
111+
android:gravity="end"
92112
app:dropdownResource="@array/bootstrap_dropdown_example_data"
93-
app:bootstrapExpandDirection="down" />
113+
app:roundedCorners="true"
114+
/>
115+
94116
<HorizontalScrollView
95117
android:layout_width="match_parent"
96118
android:layout_height="wrap_content"
97119
android:layout_alignParentBottom="true"
98-
android:scrollbars="none">
120+
android:scrollbars="none"
121+
>
122+
99123
<LinearLayout
100124
android:layout_width="match_parent"
101125
android:layout_height="wrap_content"
102-
android:orientation="horizontal">
126+
android:orientation="horizontal"
127+
>
128+
103129
<com.beardedhen.androidbootstrap.BootstrapDropDown
104130
android:layout_width="wrap_content"
105131
android:layout_height="wrap_content"
132+
app:bootstrapBrand="success"
133+
app:bootstrapExpandDirection="up"
134+
app:bootstrapSize="xs"
106135
app:bootstrapText="XSmall {fa_thumbs_o_up}"
136+
app:dropdownResource="@array/bootstrap_dropdown_example_data"
107137
app:roundedCorners="true"
108138
app:showOutline="true"
109-
app:bootstrapSize="xs"
110-
app:bootstrapBrand="success"
111-
app:dropdownResource="@array/bootstrap_dropdown_example_data"
112-
app:bootstrapExpandDirection="up" />
139+
/>
113140

114141
<com.beardedhen.androidbootstrap.BootstrapDropDown
115142
android:layout_width="wrap_content"
116143
android:layout_height="wrap_content"
117144
android:layout_marginLeft="8dp"
118-
app:bootstrapText="Small {fa_thumbs_o_up}"
119145
app:bootstrapBrand="danger"
120-
app:showOutline="true"
146+
app:bootstrapExpandDirection="up"
121147
app:bootstrapSize="sm"
122-
app:roundedCorners="true"
148+
app:bootstrapText="Small {fa_thumbs_o_up}"
123149
app:dropdownResource="@array/bootstrap_dropdown_example_data"
124-
app:bootstrapExpandDirection="up"/>
150+
app:roundedCorners="true"
151+
app:showOutline="true"
152+
/>
125153

126154
<com.beardedhen.androidbootstrap.BootstrapDropDown
127155
android:layout_width="wrap_content"
128156
android:layout_height="wrap_content"
129157
android:layout_marginLeft="8dp"
130-
app:bootstrapText="Medium {fa_thumbs_o_up}"
131158
app:bootstrapBrand="regular"
132-
app:roundedCorners="true"
133-
app:showOutline="true"
159+
app:bootstrapExpandDirection="up"
134160
app:bootstrapSize="md"
161+
app:bootstrapText="Medium {fa_thumbs_o_up}"
135162
app:dropdownResource="@array/bootstrap_dropdown_example_data"
136-
app:bootstrapExpandDirection="up"/>
163+
app:roundedCorners="true"
164+
app:showOutline="true"
165+
/>
166+
137167
<com.beardedhen.androidbootstrap.BootstrapDropDown
138168
android:layout_width="wrap_content"
139169
android:layout_height="wrap_content"
140170
android:layout_marginLeft="8dp"
141-
app:bootstrapText="Large {fa_thumbs_o_up}"
142171
app:bootstrapBrand="secondary"
143-
app:roundedCorners="true"
144-
app:showOutline="true"
172+
app:bootstrapExpandDirection="up"
145173
app:bootstrapSize="lg"
174+
app:bootstrapText="Large {fa_thumbs_o_up}"
146175
app:dropdownResource="@array/bootstrap_dropdown_example_data"
147-
app:bootstrapExpandDirection="up"/>
176+
app:roundedCorners="true"
177+
app:showOutline="true"
178+
/>
179+
148180
<com.beardedhen.androidbootstrap.BootstrapDropDown
149181
android:layout_width="wrap_content"
150182
android:layout_height="wrap_content"
151183
android:layout_marginLeft="8dp"
152-
app:bootstrapText="XLarge {fa_thumbs_o_up}"
153184
app:bootstrapBrand="warning"
154-
app:showOutline="true"
155-
app:roundedCorners="true"
185+
app:bootstrapExpandDirection="up"
156186
app:bootstrapSize="xl"
187+
app:bootstrapText="XLarge {fa_thumbs_o_up}"
157188
app:dropdownResource="@array/bootstrap_dropdown_example_data"
158-
app:bootstrapExpandDirection="up"/>
189+
app:roundedCorners="true"
190+
app:showOutline="true"
191+
/>
159192
</LinearLayout>
160193
</HorizontalScrollView>
161194
</RelativeLayout>

sample/src/main/res/values/strings.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,17 @@
99
<item>Fifth item</item>
1010
<item>{dropdown_separator}</item>
1111
<item>Separated item</item>
12+
<item>Item 9</item>
13+
<item>Item 10</item>
14+
<item>Item 11</item>
15+
<item>Item 12</item>
16+
<item>Item 13</item>
17+
<item>Item 14</item>
18+
<item>Item 15</item>
19+
<item>Item 16</item>
20+
<item>Item 17</item>
21+
<item>Item 18</item>
22+
<item>Item 19</item>
23+
<item>Item 20</item>
1224
</array>
1325
</resources>

0 commit comments

Comments
 (0)