0

I have a problem with androidx.appcompat:appcompat:1.1.0. It's a new problem because on androidx.appcompat:appcompat:1.0.2 it does not exist.

I have a code that uses reflection to get mPopup Field from the spinner and set its height. It works very well on appcompat:1.0.2 but not on androidx.appcomppat:appcompat:1.1.0.

The code is

 private void setPopUp() {

        try {
            Field popup = getPopupField();

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

            // Set popupWindow height to max - 40dp
            spinner.post(new Runnable() {
                @Override
                public void run() {
                    Rect r = new Rect();
                    spinner.getGlobalVisibleRect(r);
                    int height = 200;
                    popupWindow.setHeight(height);
                }
            });
        } catch (NoClassDefFoundError | ClassCastException | IllegalAccessException e) {
            // silently fail...
        }


    }

    private static Field getPopupField () {
        if (sPopupField == null) {
            try {
                Field popup = Spinner.class.getDeclaredField("mPopup");
                popup.setAccessible(true);

                sPopupField = popup;
            } catch (NoClassDefFoundError | ClassCastException | NoSuchFieldException e) {
                // silently fail...
            }
        }
        return sPopupField;
    }

I read about bugs on a new appcompatActivity from appcompat:1.1.0. However, I can't find a solution to my problem.

1 Answer 1

1

Everybody.

I was able to do it.

First, i put the spinner on the activity with java code. "programatically".

     public void initTest(){
    spinner2 = new Spinner(this, Spinner.MODE_DROPDOWN);
    spinner2.setAdapter(new ArrayAdapter(this, R.layout.spinner_item, datos));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        spinner2.setId(spinner2.generateViewId());
    }

    ConstraintLayout parentLayout = (ConstraintLayout)findViewById(R.id.main_activity);
    parentLayout.addView(spinner2, 0);

    ConstraintSet cs = new ConstraintSet();
    cs.clone(parentLayout);
    cs.setHorizontalBias(spinner2.getId(), 0.473F);
    cs.setVerticalBias(spinner2.getId(), 0.484F);
    cs.connect(spinner2.getId(), ConstraintSet.BOTTOM, parentLayout.getId(),ConstraintSet.BOTTOM);
    cs.connect(spinner2.getId(), ConstraintSet.START, parentLayout.getId(),ConstraintSet.START);
    cs.connect(spinner2.getId(), ConstraintSet.TOP, parentLayout.getId(),ConstraintSet.TOP);
    cs.connect(spinner2.getId(), ConstraintSet.END, parentLayout.getId(),ConstraintSet.END);
    // cs view id, else getId() returns -1

    // connect start and end point of views, in this case top of child to top of parent.
    // ... similarly add other constraints
    cs.applyTo(parentLayout);

}  

Then i call the code put at the beginning in my question.

I hope it works for many people, and to skip this error of the new version.

Regards.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.