Java Code Examples for android.widget.Spinner#setPopupBackgroundDrawable()

The following examples show how to use android.widget.Spinner#setPopupBackgroundDrawable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PXVirtualDropDownAdapter.java    From pixate-freestyle-android with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the popup window background image. Note that the size cannot be
 * determined at this point, so it's also recommended to set the
 * background-size in the CSS (batter scaling down from a bigger image than
 * scale up from a default 32x32 image).
 * 
 * @param ruleSets
 * @param contexts
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void setPopupBackgroundImage(List<PXRuleSet> ruleSets, List<PXStylerContext> contexts) {
    Spinner spinner = (Spinner) contexts.get(0).getStyleable();
    // Get the spinner adapter. We'll use it to create the states, although
    // its default implementation does not create any additional state-sets
    // (which is fine).
    PXStyleAdapter spinnerAdapter = PXStyleAdapter.getStyleAdapter(spinner);
    Map<int[], Drawable> existingPopupStates = PXDrawableUtil.getExistingStates(spinner
            .getPopupBackground());
    Drawable newPopupBackground = null;
    if (existingPopupStates == null || existingPopupStates.isEmpty()) {
        // create a new background for the popup
        newPopupBackground = PXDrawableUtil.createNewStateListDrawable(spinnerAdapter,
                ruleSets, contexts);
    } else {
        // merge or replace the states with what we have in the CSS
        newPopupBackground = PXDrawableUtil.createDrawable(spinnerAdapter,
                existingPopupStates, ruleSets, contexts);
    }
    if (newPopupBackground != null) {
        spinner.setPopupBackgroundDrawable(newPopupBackground);
    }
}
 
Example 2
Source File: GComboView.java    From geopaparazzi with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @param context               the context to use.
 * @param attrs                 attributes.
 * @param parentView            parent
 * @param label                 label
 * @param value                 value
 * @param itemsArray            the items.
 * @param constraintDescription constraints
 */
public GComboView(Context context, AttributeSet attrs, LinearLayout parentView, String label, String value,
                  String[] itemsArray, String constraintDescription) {
    super(context, attrs);

    LinearLayout textLayout = new LinearLayout(context);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(10, 10, 10, 10);
    textLayout.setLayoutParams(layoutParams);
    textLayout.setOrientation(LinearLayout.VERTICAL);
    parentView.addView(textLayout);

    TextView textView = new TextView(context);
    textView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    textView.setPadding(2, 2, 2, 2);
    textView.setText(label.replace(UNDERSCORE, " ").replace(COLON, " ") + " " + constraintDescription);
    textView.setTextColor(Compat.getColor(context, R.color.formcolor));
    textLayout.addView(textView);

    spinner = new Spinner(context);
    LinearLayout.LayoutParams spinnerParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    spinnerParams.setMargins(15, 25, 15, 15);
    spinner.setLayoutParams(spinnerParams);
    spinner.setPopupBackgroundDrawable(Compat.getDrawable(context, R.drawable.thin_background_frame));
    spinner.setBackground(Compat.getDrawable(context, R.drawable.thin_background_frame));
    int minHeight = 48;
    if (context instanceof Activity) {
        Activity activity = (Activity) context;
        android.util.TypedValue tv = new android.util.TypedValue();
        activity.getTheme().resolveAttribute(android.R.attr.listPreferredItemHeight, tv, true);
        android.util.DisplayMetrics metrics = new android.util.DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        float ret = tv.getDimension(metrics);

        minHeight = (int) (ret - 1 * metrics.density);
    }
    spinner.setMinimumHeight(minHeight);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, itemsArray);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    if (value != null) {
        for (int i = 0; i < itemsArray.length; i++) {
            if (itemsArray[i].equals(value.trim())) {
                spinner.setSelection(i);
                break;
            }
        }
    }

    textLayout.addView(spinner);
}
 
Example 3
Source File: GTwoConnectedComboView.java    From geopaparazzi with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @param context               the context to use.
 * @param attrs                 attributes.
 * @param parentView            parent
 * @param label                 label
 * @param value                 _value
 * @param dataMap               the map of the data.
 * @param constraintDescription constraints
 */
public GTwoConnectedComboView(Context context, AttributeSet attrs, LinearLayout parentView, String label, String value,
                              LinkedHashMap<String, List<String>> dataMap, String constraintDescription) {
    super(context, attrs);
    this.dataMap = dataMap;

    if (value == null)
        value = "";

    String[] valueSplit = value.split(SEP);
    String _combo1Value;
    String _combo2Value;
    if (valueSplit.length == 2) {
        _combo1Value = valueSplit[0];
        _combo2Value = valueSplit[1];
    } else {
        _combo1Value = "";
        _combo2Value = "";
    }

    TextView textView = new TextView(context);
    LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    textViewParams.setMargins(15, 25, 15, 15);
    textView.setLayoutParams(textViewParams);
    textView.setPadding(2, 2, 2, 2);
    textView.setText(label.replace(UNDERSCORE, " ").replace(COLON, " ") + " " + constraintDescription);
    textView.setTextColor(Compat.getColor(context, R.color.formcolor));
    parentView.addView(textView);

    LinearLayout combosLayout = new LinearLayout(context);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(10, 10, 10, 10);
    combosLayout.setLayoutParams(layoutParams);
    combosLayout.setOrientation(LinearLayout.VERTICAL);
    combosLayout.setBackground(Compat.getDrawable(context, R.drawable.thin_background_frame));
    parentView.addView(combosLayout);

    combo1Spinner = new Spinner(context);
    LinearLayout.LayoutParams titleSpinnerParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    titleSpinnerParams.setMargins(15, 25, 15, 15);
    combo1Spinner.setLayoutParams(titleSpinnerParams);
    Set<String> titlesSet = dataMap.keySet();
    ArrayList<String> combo1Items = new ArrayList<>(titlesSet);
    combo1Items.add(0, "");
    ArrayAdapter<String> titleListAdapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_dropdown_item, combo1Items);
    titleListAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    combo1Spinner.setAdapter(titleListAdapter);
    combo1Spinner.setPopupBackgroundDrawable(Compat.getDrawable(context, R.drawable.thin_background_frame));
    combo1Spinner.setBackground(Compat.getDrawable(context, R.drawable.thin_background_frame));
    int minHeight = getMinComboHeight(context);
    combo1Spinner.setMinimumHeight(minHeight);

    combo2Spinner = new Spinner(context);
    LinearLayout.LayoutParams valueSpinnerParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    valueSpinnerParams.setMargins(15, 25, 15, 15);
    combo2Spinner.setLayoutParams(valueSpinnerParams);
    combo2Spinner.setPopupBackgroundDrawable(Compat.getDrawable(context, R.drawable.thin_background_frame));
    combo2Spinner.setBackground(Compat.getDrawable(context, R.drawable.thin_background_frame));
    combo2Spinner.setMinimumHeight(minHeight);

    List<String> combo2ItemsList = new ArrayList<>();
    if (_combo1Value.length() > 0) {
        combo2ItemsList = dataMap.get(_combo1Value);
        int indexOf = combo1Items.indexOf(_combo1Value.trim());
        if (indexOf != -1) {
            combo1Spinner.setSelection(indexOf, false);
        }
    }
    ArrayAdapter<String> combo2ListAdapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_dropdown_item, combo2ItemsList);
    combo2ListAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    combo2Spinner.setAdapter(combo2ListAdapter);

    combosLayout.addView(combo1Spinner);
    combosLayout.addView(combo2Spinner);

    if (_combo2Value.length() > 0) {
        int position = combo2ListAdapter.getPosition(_combo2Value);
        combo2Spinner.setSelection(position, false);
    }

    combo1Spinner.setOnItemSelectedListener(this);
}