Search code examples
androidvisibilityandroid-themeandroid-holo-everywhere

setVisibility() is not refreshing the activity layout


I am setting the visibility of a button as VISIBLE or GONE on spinner item selection:-

selectUserTypeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                FilterUserType newUserType = FilterUserType.get(position);
                if(filter.userType != newUserType){
                    filter.userType = newUserType;
                    ScrollView mainScrollView =  (ScrollView) findViewById(R.id.mainLayout);
                    switch (newUserType) {
                    case AnyUser:
                    case CurrentUser:{
                        selectUserBtn.setVisibility(View.GONE);
                        break;
                    }
                    case SpecificUser:{
                        selectUserBtn.setVisibility(View.VISIBLE);
                        break;
                    }
                    default:
                        break;
                    }
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {}

        });

Here the problem is that my view is not refreshing after this action, but when I select one EditText on the page and keyboard is up.. at that time it refreshes and shows the desired behaviour.

I have tried editing switch-case as:-

switch (newUserType) {
    case AnyUser:
    case CurrentUser:{
        selectUserBtn.setVisibility(View.GONE);
        mainScrollView.invalidate();
        break;
    }
    case SpecificUser:{
        selectUserBtn.setVisibility(View.VISIBLE);
        mainScrollView.invalidate();
        break;
    }
    default:
        break;
    }
}

But this is also not working

FYI, I am using HoloEverywhere theme in my project and this spinner is from HoloEverywhere widgets.


Solution

  • I had the same problem. The spinner class from holoeverywhere updates its graphics after the onItemSelected is called. So it "overrides" your layout update. The only solution I found was to set a handler to update the ui, to be exececuted 1 second in the future. Like this:

    Handler han = new Handler();
            han.postAtTime(new Runnable() {
                @Override
                public void run() {
                    // UPDATE UI HERE
                }
            }, 1000);