Skip to content
This repository has been archived by the owner on Jul 28, 2024. It is now read-only.

Commit

Permalink
Improve popup window's height computation
Browse files Browse the repository at this point in the history
  • Loading branch information
arcadefire committed Oct 8, 2017
1 parent 7a643ef commit 3d3992f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ apply plugin: 'com.android.library'
buildscript {
repositories {
jcenter()
google()
maven {
url 'https://maven.google.com'
}
Expand Down
44 changes: 36 additions & 8 deletions src/main/java/org/angmarch/views/NiceSpinner.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.angmarch.views;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
Expand All @@ -16,6 +17,7 @@
import android.support.v4.view.animation.LinearOutSlowInInterpolator;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
Expand Down Expand Up @@ -51,6 +53,7 @@ public class NiceSpinner extends AppCompatTextView {
private static final String IS_POPUP_SHOWING = "is_popup_showing";
private static final String IS_ARROW_HIDDEN = "is_arrow_hidden";
private static final String ARROW_DRAWABLE_RES_ID = "arrow_drawable_res_id";
public static final int VERTICAL_OFFSET = 1;

private int selectedIndex;
private Drawable arrowDrawable;
Expand All @@ -63,9 +66,10 @@ public class NiceSpinner extends AppCompatTextView {
private int textColor;
private int backgroundSelector;
private int arrowDrawableTint;
private int displayHeight;
private int parentVerticalOffset;
private int dropDownListPaddingBottom;
private @DrawableRes int arrowDrawableResId;

private SpinnerTextFormatter spinnerTextFormatter = new SimpleSpinnerTextFormatter();

public NiceSpinner(Context context) {
Expand Down Expand Up @@ -201,6 +205,26 @@ public void onDismiss() {
dropDownListPaddingBottom =
typedArray.getDimensionPixelSize(R.styleable.NiceSpinner_dropDownListPaddingBottom, 0);
typedArray.recycle();

measureDisplayHeight();
}

private void measureDisplayHeight() {
DisplayMetrics displayMetrics = new DisplayMetrics();
Activity activity = (Activity) getContext();
activity.getWindowManager()
.getDefaultDisplay()
.getMetrics(displayMetrics);
displayHeight = displayMetrics.heightPixels;
}

private int getParentVerticalOffset() {
if (parentVerticalOffset > 0) {
return parentVerticalOffset;
}
int[] locationOnScreen = new int[2];
getLocationOnScreen(locationOnScreen);
return parentVerticalOffset = locationOnScreen[VERTICAL_OFFSET];
}

@Override protected void onVisibilityChanged(View changedView, int visibility) {
Expand Down Expand Up @@ -298,13 +322,6 @@ private void setAdapterInternal(NiceSpinnerBaseAdapter adapter) {
setText(adapter.getItemInDataset(selectedIndex).toString());
}

@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
listView.measure(widthMeasureSpec, heightMeasureSpec);
popupWindow.setWidth(View.MeasureSpec.getSize(widthMeasureSpec));
popupWindow.setHeight(listView.getMeasuredHeight() - getMeasuredHeight() - dropDownListPaddingBottom);
}

@Override public boolean onTouchEvent(MotionEvent event) {
if (isEnabled() && event.getAction() == MotionEvent.ACTION_UP) {
if (!popupWindow.isShowing()) {
Expand Down Expand Up @@ -335,9 +352,20 @@ public void showDropDown() {
if (!isArrowHidden) {
animateArrow(true);
}
measurePopUpDimension();
popupWindow.showAsDropDown(this);
}

private void measurePopUpDimension() {
int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
int heightSpec = MeasureSpec.makeMeasureSpec(
displayHeight - getParentVerticalOffset() - getMeasuredHeight(),
MeasureSpec.AT_MOST);
listView.measure(widthSpec, heightSpec);
popupWindow.setWidth(listView.getMeasuredWidth());
popupWindow.setHeight(listView.getMeasuredHeight() - dropDownListPaddingBottom);
}

public void setTintColor(@ColorRes int resId) {
if (arrowDrawable != null && !isArrowHidden) {
DrawableCompat.setTint(arrowDrawable, ContextCompat.getColor(getContext(), resId));
Expand Down

0 comments on commit 3d3992f

Please sign in to comment.