@@ -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
0 commit comments