Change the field visibility of a layout at run time

0

In a layout for customer registration, there are specific fields for Corporate and Individual. The control of the type of register is done by RadioButton that are within RadioGroup .

I'm trying to change the RadioButton property of the fields to Visibility when an option is checked, but nothing is happening.

Is it possible to perform this control through gone ?

Follow code from RadioButton

RadioGroup group = (RadioGroup) findViewById(R.id.rgTipoEmpresa);
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                boolean rbFisica = R.id.rbFisica == checkedId;
                boolean rbJuridica = R.id.rbJuridica == checkedId;

                if (rbFisica){
                    etCNPJ.setVisibility(2);
                    etInscricao.setVisibility(2);
                    tvCNPJ.setVisibility(2);
                    tvInscricao.setVisibility(2);
                }

                if (rbJuridica){
                    etCPF.setVisibility(2);
                    etRG.setVisibility(2);
                    tvCPF.setVisibility(2);
                    tvRG.setVisibility(2);

                }
            }
        });
    
asked by anonymous 10.12.2014 / 13:14

1 answer

3

Do not use values explicitly when there are constants defined for this purpose. The View class declares the following constants, to be used in the setVisibility() method, to define its visibility:

View.VISIBLE    // valor 0
View.INVISIBLE  // valor 4
View.GONE       // valor 8

Using the constants avoids errors and makes the code more readable.

etCNPJ.setVisibility(View.GONE);

In your code you use a value of 2 that is not a valid value. ( View Documentation ).

Do not forget also that when you select a RadioButton , in addition to making "GONE" , items that do not apply to this situation will have to become "VISIBLE" > the items that apply.

A simple way to invert visibility is to use the ^ (XOR) operator:

etCNPJ.setVisibility(etCNPJ.getVisibility() ^ View.GONE);

After the execution of this line etCNPJ will become VISIBLE if GONE or GONE / em>.

    
10.12.2014 / 15:34