Skip to content

Commit

Permalink
add ability to set the PopupWindow height (see #11)
Browse files Browse the repository at this point in the history
Also,
- update build tools to 23.0.3
- update support-annotations to 23.3.0
- maven 1.0.8
  • Loading branch information
jaredrummler committed May 11, 2016
1 parent dec97df commit 1c20216
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 18 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,29 @@ spinner.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<Str

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

| name | type | info |
|---------------------|---------|--------------------------------------------------------|
| ms_arrow_tint | color | sets the color on the drop-down arrow |
| ms_hide_arrow | boolean | set to true to hide the arrow drawable |
| ms_background_color | color | set the background color for the spinner and drop-down |
| ms_text_color | color | set the text color |
| name | type | info |
|------------------------|-----------|--------------------------------------------------------|
| ms_arrow_tint | color | sets the color on the drop-down arrow |
| ms_hide_arrow | boolean | set to true to hide the arrow drawable |
| ms_background_color | color | set the background color for the spinner and drop-down |
| ms_text_color | color | set the text color |
| ms_dropdown_max_height | dimension | set the max height of the drop-down |
| ms_dropdown_height | dimension | set the height of the drop-down |

Download
--------

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:
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:

```groovy
compile 'com.jaredrummler:material-spinner:1.0.7'
compile 'com.jaredrummler:material-spinner:1.0.8'
```
or Maven:
```xml
<dependency>
<groupId>com.jaredrummler</groupId>
<artifactId>material-spinner</artifactId>
<version>1.0.7</version>
<version>1.0.8</version>
<type>aar</type>
</dependency>
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@

public class MainActivity extends AppCompatActivity {

private static final String[] ANDROID_VERSIONS = {
"Cupcake", "Donut", "Eclair", "Froyo", "Gingerbread", "Honeycomb", "Ice Cream Sandwich", "Jelly Bean", "KitKat",
"Lollipop", "Marshmallow"
};

@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Expand All @@ -49,7 +54,7 @@ public class MainActivity extends AppCompatActivity {
});

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

@Override public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

<com.jaredrummler.materialspinner.MaterialSpinner
android:id="@+id/spinner"
app:ms_dropdown_max_height="350dp"
app:ms_dropdown_height="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
#
#

VERSION_NAME=1.0.7
VERSION_CODE=8
VERSION_NAME=1.0.8
VERSION_CODE=9
GROUP=com.jaredrummler

POM_NAME=Material Spinner
Expand Down
4 changes: 2 additions & 2 deletions lib-materialspinner/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ apply plugin: 'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
buildToolsVersion "23.0.3"
resourcePrefix 'ms__'

defaultConfig {
Expand All @@ -30,7 +30,7 @@ android {

dependencies {
testCompile 'junit:junit:4.12'
compile 'com.android.support:support-annotations:23.2.0'
compile 'com.android.support:support-annotations:23.3.0'
}

apply from: 'https://raw.github.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public class MaterialSpinner extends TextView {
private Drawable arrowDrawable;
private boolean hideArrow;
private boolean nothingSelected;
private int popupWindowMaxHeight;
private int popupWindowHeight;
private int selectedIndex;
private int backgroundColor;
private int arrowColor;
Expand Down Expand Up @@ -93,6 +95,9 @@ private void init(Context context, AttributeSet attrs) {
textColor = ta.getColor(R.styleable.MaterialSpinner_ms_text_color, defaultColor);
arrowColor = ta.getColor(R.styleable.MaterialSpinner_ms_arrow_tint, textColor);
hideArrow = ta.getBoolean(R.styleable.MaterialSpinner_ms_hide_arrow, false);
popupWindowMaxHeight = ta.getDimensionPixelSize(R.styleable.MaterialSpinner_ms_dropdown_max_height, 0);
popupWindowHeight = ta.getLayoutDimension(R.styleable.MaterialSpinner_ms_dropdown_height,
WindowManager.LayoutParams.WRAP_CONTENT);
arrowColorDisabled = Utils.lighter(arrowColor, 0.8f);
} finally {
ta.recycle();
Expand Down Expand Up @@ -183,7 +188,7 @@ private void init(Context context, AttributeSet attrs) {

@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
popupWindow.setWidth(MeasureSpec.getSize(widthMeasureSpec));
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(calculatePopupWindowHeight());
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

Expand Down Expand Up @@ -411,6 +416,40 @@ private void animateArrow(boolean shouldRotateUp) {
animator.start();
}

/**
* Set the maximum height of the dropdown menu.
*
* @param height
* the height in pixels
*/
public void setDropdownMaxHeight(int height) {
popupWindowMaxHeight = height;
popupWindow.setHeight(calculatePopupWindowHeight());
}

/**
* Set the height of the dropdown menu
*
* @param height
* the height in pixels
*/
public void setDropdownHeight(int height) {
popupWindowHeight = height;
popupWindow.setHeight(calculatePopupWindowHeight());
}

private int calculatePopupWindowHeight() {
float listViewHeight = adapter.getCount() * getResources().getDimension(R.dimen.ms__item_height);
if (popupWindowMaxHeight > 0 && listViewHeight > popupWindowMaxHeight) {
return popupWindowMaxHeight;
} else if (popupWindowHeight != WindowManager.LayoutParams.MATCH_PARENT
&& popupWindowHeight != WindowManager.LayoutParams.WRAP_CONTENT
&& popupWindowHeight <= listViewHeight) {
return popupWindowHeight;
}
return WindowManager.LayoutParams.WRAP_CONTENT;
}

/**
* Get the {@link PopupWindow}.
*
Expand Down
2 changes: 1 addition & 1 deletion lib-materialspinner/src/main/res/layout/ms__list_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
android:layout_gravity="start|center_vertical"
android:ellipsize="marquee"
android:gravity="center_vertical"
android:minHeight="48dp"
android:minHeight="@dimen/ms__item_height"
android:paddingBottom="@dimen/ms__padding_top"
android:paddingLeft="@dimen/ms__padding_left"
android:paddingRight="@dimen/ms__padding_left"
Expand Down
6 changes: 6 additions & 0 deletions lib-materialspinner/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,11 @@
<attr name="ms_hide_arrow" format="boolean"/>
<attr name="ms_background_color" format="color"/>
<attr name="ms_text_color" format="color"/>
<attr name="ms_dropdown_max_height" format="dimension"/>
<attr name="ms_dropdown_height" format="dimension">
<enum name="fill_parent" value="-1"/>
<enum name="match_parent" value="-1"/>
<enum name="wrap_content" value="-2"/>
</attr>
</declare-styleable>
</resources>
5 changes: 3 additions & 2 deletions lib-materialspinner/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
-->

<resources>
<dimen name="ms__padding_left">24dp</dimen>
<dimen name="ms__padding_top">12dp</dimen>
<dimen name="ms__padding_left">24dp</dimen>
<dimen name="ms__padding_top">12dp</dimen>
<dimen name="ms__item_height">48dp</dimen>
</resources>

0 comments on commit 1c20216

Please sign in to comment.