Invisible RadioButton with spinner

1

I would like to know how to make a spinner's event change a radiobutton visible or invisible. I have the following code. But it is not working.

 <RadioGroup 
                       android:id="@+id/radioGrupo"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:visibility="invisible"
                        android:orientation="horizontal">

                        <RadioButton
                            android:id="@+id/radioUsuario"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:visibility="invisible"
                            android:onClick="onRadioButtonClicked"
                            android:text="Usuário" />

                        <RadioButton
                            android:id="@+id/radioNaoUsuario"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:onClick="onRadioButtonClicked"
                            android:visibility="invisible"
                            android:text="Não usuário" />

                    </RadioGroup> 

private void ExibeUsuarioNaoUsuario()
{
    naoUsuario =(RadioButton) findViewById(R.id.radioNaoUsuario);
    usuario =(RadioButton) findViewById(R.id.radioUsuario);
    groupUsuarioNaoUsuario =(RadioGroup)findViewById(R.id.radioGrupo);
    spnTipoCliente = (Spinner)findViewById(R.id.spnTipoCliente);
    spnTipoCliente.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {

            groupUsuarioNaoUsuario.setVisibility(RadioGroup.VISIBLE);
            //usuario.setVisibility(RadioButton.VISIBLE);
            //naoUsuario.setVisibility(RadioButton.VISIBLE);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            usuario.setVisibility(RadioButton.INVISIBLE);
            naoUsuario.setVisibility(RadioButton.INVISIBLE);
        }

    });

}
    
asked by anonymous 02.01.2015 / 23:31

1 answer

2

Using setVisibility(View.INVISIBLE) works in RadioGroup , it's even better to set visibility for all RadioButton's .

The problem is understanding the onNothingSelected .

I did not really know until now, but it is hardly called when the user touches on Spinner , but when the user's previous selection ceases to exist.

  

Callback method to be invoked when the selection disappears from this view. The selection can disappear for instance when touch is activated or when the adapter is empty.

This can be seen when you clear Adapter with any previously selected option.

If your logic is to hide when an item is selected, perhaps the best solution is to check the parameter position or even the label if it is something more secure.

    
03.01.2015 / 18:12