Skip to content

Commit

Permalink
Fixed spinner height to 12 items
Browse files Browse the repository at this point in the history
Signed-off-by: Ephraim Muhia <emuhia@ona.io>
  • Loading branch information
Ephraim Muhia committed Feb 2, 2018
1 parent c20d607 commit 500412c
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package org.smartregister.path.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.util.Pair;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.apache.commons.lang3.time.DateUtils;
import org.smartregister.domain.FetchStatus;
import org.smartregister.immunization.db.VaccineRepo;
import org.smartregister.path.R;
Expand All @@ -22,19 +24,14 @@
import org.smartregister.path.repository.CohortPatientRepository;
import org.smartregister.path.repository.CohortRepository;
import org.smartregister.path.toolbar.LocationSwitcherToolbar;
import org.smartregister.path.view.CustomHeightSpinner;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import util.PathConstants;

/**
* Created by keyman on 21/12/17.
*/
Expand Down Expand Up @@ -124,6 +121,21 @@ private void updateCohortSize() {
textView.setText(String.format(getString(R.string.cso_population_value), size));
}

private void updateSpinnerSize(List list) {
if (list != null && list.size() > 12) {
TypedValue typedValue = new TypedValue();
CohortCoverageReportActivity.this.getTheme().resolveAttribute(android.R.attr.textAppearanceLarge, typedValue, true);

int[] attribute = new int[]{R.attr.dropdownListPreferredItemHeight};
TypedArray array = CohortCoverageReportActivity.this.obtainStyledAttributes(typedValue.resourceId, attribute);
int heightOfDropoutItem = array.getDimensionPixelSize(0, -1);
array.recycle();

CustomHeightSpinner customHeightSpinner = (CustomHeightSpinner) findViewById(R.id.report_spinner);
customHeightSpinner.updateHeight(heightOfDropoutItem, 12);
}
}

@Override
public void onServiceFinish(String actionType) {
if (CoverageDropoutBroadcastReceiver.TYPE_GENERATE_COHORT_INDICATORS.equals(actionType)) {
Expand Down Expand Up @@ -245,6 +257,7 @@ protected void generateReportUI(Map<String, NamedObject<?>> map, boolean userAct
}

updateReportDates(cohorts, new SimpleDateFormat("MMM yyyy"), null, true);
updateSpinnerSize(cohorts);
updateCohortSize();
updateReportList(indicatorList);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.smartregister.path.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.Spinner;

import java.lang.reflect.Field;

/**
* Created by keyman on 2/2/18.
*/

@SuppressLint("AppCompatCustomView")
public class CustomHeightSpinner extends Spinner {
private static final String TAG = CustomHeightSpinner.class.getName();

public CustomHeightSpinner(Context context) {
super(context);
}

public CustomHeightSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}

public CustomHeightSpinner(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

public void updateHeight(final int height, final int itemCount) {
try {
final Field popup = Spinner.class.getDeclaredField("mPopup");
popup.setAccessible(true);

// Get private mPopup member variable and try cast to ListPopupWindow
final android.widget.ListPopupWindow popupWindow = (android.widget.ListPopupWindow) popup.get(this);

// Set popupWindow height to max - 40dp
this.post(new Runnable() {
@Override
public void run() {
int heightToUse = height;
if (heightToUse == -1) {
heightToUse = CustomHeightSpinner.this.getHeight();
}
popupWindow.setHeight(heightToUse * itemCount);
popup.setAccessible(false);
}
});
} catch (NoClassDefFoundError | ClassCastException | NoSuchFieldException | IllegalAccessException e) {
Log.e(TAG, e.getMessage(), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Iterator;

import util.JsonFormUtils;
import util.Utils;

/**
* @author Jason Rogena - jrogena@ona.io
Expand Down Expand Up @@ -192,17 +193,11 @@ private void showDialog() {
LocationPickerView.this.getLocationInWindow(coords);
wlp.x = coords[0]
+ (int) (LocationPickerView.this.getWidth() * 0.5)
- (int) (convertDpToPx(780) * 0.5);
- (int) (Utils.convertDpToPx(context, 780) * 0.5);

locationPickerDialog.show();
}

private int convertDpToPx(int dp) {
Resources r = context.getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
return Math.round(px);
}

public interface OnLocationChangeListener {
void onLocationChange(String newLocation);
}
Expand Down
8 changes: 8 additions & 0 deletions opensrp-path/src/main/java/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
package util;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.text.Html;
import android.text.InputType;
import android.text.Spanned;
import android.util.Log;
import android.util.TypedValue;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TableRow;
Expand Down Expand Up @@ -317,4 +319,10 @@ public static Date dobStringToDate(String dobString) {
return null;
}
}

public static int convertDpToPx(Context context, int dp) {
Resources r = context.getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
return Math.round(px);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@
android:textColor="@color/text_black"
android:textSize="20sp" />

<Spinner
<org.smartregister.path.view.CustomHeightSpinner
android:id="@+id/report_spinner"
style="@style/PathSpinnerUnderLined"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:minWidth="170dp"
android:scrollbars="vertical"
android:spinnerMode="dropdown"
android:theme="@style/ThemePathSpinner" />

Expand Down

0 comments on commit 500412c

Please sign in to comment.