Skip to content

Commit 1c20216

Browse files
committed
add ability to set the PopupWindow height (see #11)
Also, - update build tools to 23.0.3 - update support-annotations to 23.3.0 - maven 1.0.8
1 parent dec97df commit 1c20216

File tree

9 files changed

+73
-18
lines changed

9 files changed

+73
-18
lines changed

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,29 @@ spinner.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<Str
3232

3333
You can add attributes to customize the view. Available attributes:
3434

35-
| name | type | info |
36-
|---------------------|---------|--------------------------------------------------------|
37-
| ms_arrow_tint | color | sets the color on the drop-down arrow |
38-
| ms_hide_arrow | boolean | set to true to hide the arrow drawable |
39-
| ms_background_color | color | set the background color for the spinner and drop-down |
40-
| ms_text_color | color | set the text color |
35+
| name | type | info |
36+
|------------------------|-----------|--------------------------------------------------------|
37+
| ms_arrow_tint | color | sets the color on the drop-down arrow |
38+
| ms_hide_arrow | boolean | set to true to hide the arrow drawable |
39+
| ms_background_color | color | set the background color for the spinner and drop-down |
40+
| ms_text_color | color | set the text color |
41+
| ms_dropdown_max_height | dimension | set the max height of the drop-down |
42+
| ms_dropdown_height | dimension | set the height of the drop-down |
4143

4244
Download
4345
--------
4446

45-
Download [the latest AAR](https://repo1.maven.org/maven2/com/jaredrummler/material-spinner/1.0.7/material-spinner-1.0.7.aar) or grab via Gradle:
47+
Download [the latest AAR](https://repo1.maven.org/maven2/com/jaredrummler/material-spinner/1.0.8/material-spinner-1.0.8.aar) or grab via Gradle:
4648

4749
```groovy
48-
compile 'com.jaredrummler:material-spinner:1.0.7'
50+
compile 'com.jaredrummler:material-spinner:1.0.8'
4951
```
5052
or Maven:
5153
```xml
5254
<dependency>
5355
<groupId>com.jaredrummler</groupId>
5456
<artifactId>material-spinner</artifactId>
55-
<version>1.0.7</version>
57+
<version>1.0.8</version>
5658
<type>aar</type>
5759
</dependency>
5860
```

app/src/main/java/com/jaredrummler/materialspinner/example/MainActivity.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131

3232
public class MainActivity extends AppCompatActivity {
3333

34+
private static final String[] ANDROID_VERSIONS = {
35+
"Cupcake", "Donut", "Eclair", "Froyo", "Gingerbread", "Honeycomb", "Ice Cream Sandwich", "Jelly Bean", "KitKat",
36+
"Lollipop", "Marshmallow"
37+
};
38+
3439
@Override protected void onCreate(Bundle savedInstanceState) {
3540
super.onCreate(savedInstanceState);
3641
setContentView(R.layout.activity_main);
@@ -49,7 +54,7 @@ public class MainActivity extends AppCompatActivity {
4954
});
5055

5156
MaterialSpinner spinner = (MaterialSpinner) findViewById(R.id.spinner);
52-
spinner.setItems("Ice Cream Sandwich", "Jelly Bean", "KitKat", "Lollipop", "Marshmallow");
57+
spinner.setItems(ANDROID_VERSIONS);
5358
spinner.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {
5459

5560
@Override public void onItemSelected(MaterialSpinner view, int position, long id, String item) {

app/src/main/res/layout/content_main.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
<com.jaredrummler.materialspinner.MaterialSpinner
3434
android:id="@+id/spinner"
35+
app:ms_dropdown_max_height="350dp"
36+
app:ms_dropdown_height="wrap_content"
3537
android:layout_width="match_parent"
3638
android:layout_height="wrap_content"/>
3739

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
#
1616
#
1717

18-
VERSION_NAME=1.0.7
19-
VERSION_CODE=8
18+
VERSION_NAME=1.0.8
19+
VERSION_CODE=9
2020
GROUP=com.jaredrummler
2121

2222
POM_NAME=Material Spinner

lib-materialspinner/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ apply plugin: 'com.android.library'
1919

2020
android {
2121
compileSdkVersion 23
22-
buildToolsVersion "23.0.2"
22+
buildToolsVersion "23.0.3"
2323
resourcePrefix 'ms__'
2424

2525
defaultConfig {
@@ -30,7 +30,7 @@ android {
3030

3131
dependencies {
3232
testCompile 'junit:junit:4.12'
33-
compile 'com.android.support:support-annotations:23.2.0'
33+
compile 'com.android.support:support-annotations:23.3.0'
3434
}
3535

3636
apply from: 'https://raw.github.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle'

lib-materialspinner/src/main/java/com/jaredrummler/materialspinner/MaterialSpinner.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public class MaterialSpinner extends TextView {
6161
private Drawable arrowDrawable;
6262
private boolean hideArrow;
6363
private boolean nothingSelected;
64+
private int popupWindowMaxHeight;
65+
private int popupWindowHeight;
6466
private int selectedIndex;
6567
private int backgroundColor;
6668
private int arrowColor;
@@ -93,6 +95,9 @@ private void init(Context context, AttributeSet attrs) {
9395
textColor = ta.getColor(R.styleable.MaterialSpinner_ms_text_color, defaultColor);
9496
arrowColor = ta.getColor(R.styleable.MaterialSpinner_ms_arrow_tint, textColor);
9597
hideArrow = ta.getBoolean(R.styleable.MaterialSpinner_ms_hide_arrow, false);
98+
popupWindowMaxHeight = ta.getDimensionPixelSize(R.styleable.MaterialSpinner_ms_dropdown_max_height, 0);
99+
popupWindowHeight = ta.getLayoutDimension(R.styleable.MaterialSpinner_ms_dropdown_height,
100+
WindowManager.LayoutParams.WRAP_CONTENT);
96101
arrowColorDisabled = Utils.lighter(arrowColor, 0.8f);
97102
} finally {
98103
ta.recycle();
@@ -183,7 +188,7 @@ private void init(Context context, AttributeSet attrs) {
183188

184189
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
185190
popupWindow.setWidth(MeasureSpec.getSize(widthMeasureSpec));
186-
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
191+
popupWindow.setHeight(calculatePopupWindowHeight());
187192
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
188193
}
189194

@@ -411,6 +416,40 @@ private void animateArrow(boolean shouldRotateUp) {
411416
animator.start();
412417
}
413418

419+
/**
420+
* Set the maximum height of the dropdown menu.
421+
*
422+
* @param height
423+
* the height in pixels
424+
*/
425+
public void setDropdownMaxHeight(int height) {
426+
popupWindowMaxHeight = height;
427+
popupWindow.setHeight(calculatePopupWindowHeight());
428+
}
429+
430+
/**
431+
* Set the height of the dropdown menu
432+
*
433+
* @param height
434+
* the height in pixels
435+
*/
436+
public void setDropdownHeight(int height) {
437+
popupWindowHeight = height;
438+
popupWindow.setHeight(calculatePopupWindowHeight());
439+
}
440+
441+
private int calculatePopupWindowHeight() {
442+
float listViewHeight = adapter.getCount() * getResources().getDimension(R.dimen.ms__item_height);
443+
if (popupWindowMaxHeight > 0 && listViewHeight > popupWindowMaxHeight) {
444+
return popupWindowMaxHeight;
445+
} else if (popupWindowHeight != WindowManager.LayoutParams.MATCH_PARENT
446+
&& popupWindowHeight != WindowManager.LayoutParams.WRAP_CONTENT
447+
&& popupWindowHeight <= listViewHeight) {
448+
return popupWindowHeight;
449+
}
450+
return WindowManager.LayoutParams.WRAP_CONTENT;
451+
}
452+
414453
/**
415454
* Get the {@link PopupWindow}.
416455
*

lib-materialspinner/src/main/res/layout/ms__list_item.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
android:layout_gravity="start|center_vertical"
2525
android:ellipsize="marquee"
2626
android:gravity="center_vertical"
27-
android:minHeight="48dp"
27+
android:minHeight="@dimen/ms__item_height"
2828
android:paddingBottom="@dimen/ms__padding_top"
2929
android:paddingLeft="@dimen/ms__padding_left"
3030
android:paddingRight="@dimen/ms__padding_left"

lib-materialspinner/src/main/res/values/attrs.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,11 @@
2222
<attr name="ms_hide_arrow" format="boolean"/>
2323
<attr name="ms_background_color" format="color"/>
2424
<attr name="ms_text_color" format="color"/>
25+
<attr name="ms_dropdown_max_height" format="dimension"/>
26+
<attr name="ms_dropdown_height" format="dimension">
27+
<enum name="fill_parent" value="-1"/>
28+
<enum name="match_parent" value="-1"/>
29+
<enum name="wrap_content" value="-2"/>
30+
</attr>
2531
</declare-styleable>
2632
</resources>

lib-materialspinner/src/main/res/values/dimens.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
-->
1818

1919
<resources>
20-
<dimen name="ms__padding_left">24dp</dimen>
21-
<dimen name="ms__padding_top">12dp</dimen>
20+
<dimen name="ms__padding_left">24dp</dimen>
21+
<dimen name="ms__padding_top">12dp</dimen>
22+
<dimen name="ms__item_height">48dp</dimen>
2223
</resources>

0 commit comments

Comments
 (0)