Search code examples
androidandroid-edittextvisibleandroid-orientationinvisible

VISIBLE EditText goes to INVISBLE on orientation change


I have a spinner that has numbers from 1 - 10. If the user selects anything above 1, a certain EditText that is INVISBLE becomes VISIBLE, this is what I want. But when I change it from portrait to landscape, it goes back to INVISIBLE.

This is where the changing happens

 btn_Calc.setOnClickListener(new View.OnClickListener() {
        @Override
        if (split > 1) {
            et_APP.setVisibility(View.VISIBLE);
            tv_app.setVisibility(View.VISIBLE);
            vis = 1;
            }
            else{
                et_APP.setVisibility(View.INVISIBLE);
                tv_app.setVisibility(View.INVISIBLE);
                vis = 2;
                    }
                });

And I tried to do onSaveInstanceState like this

if(savedInstanceState != null) {

        int isVis = savedInstanceState.getInt("vis", 2);
        if(isVis == 1){
            et_APP.setVisibility(View.VISIBLE);
            tv_app.setVisibility(View.VISIBLE);
        }
        else{
            et_APP.setVisibility(View.INVISIBLE);
            tv_app.setVisibility(View.INVISIBLE);
        }
    }

public void onSaveInstanceState(Bundle savedInstanceState){
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putInt("getVisible", vis);
}

Sorry for poor formatting, I cut out unrelated code.

It works that if the selected item in the spinner is greater than 1 and I click the calc button, it shows the fields I want but when I change orientation they dissapear again. Any ideas on how I can get to stay visible when I change the orientation?


Solution

  • The names of your keys don't match between save and load.

    Saving:

    savedInstanceState.putInt("getVisible", vis);
    

    Restoring:

    int isVis = savedInstanceState.getInt("vis", 2);