Error getting RadioGroup Android Id

0

I'm having difficulty getting the Radio Group ID. Can someone explain to me how it works?

public class MainActivity extends AppCompatActivity {

private static String TAG = "livro";
private ToggleButton toggleButton;
private Button botao;
String idSelecionado;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.group);

    idSelecionado = String.valueOf( radioGroup.getCheckedRadioButtonId());

    botao = (Button) findViewById(R.id.btnSalvar);

    botao.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Toast.makeText(getApplicationContext(),idSelecionado,Toast.LENGTH_LONG).show();
        }
    });
}
}

=== > > > Toast is returning -1 on any selected option.

---------------------------- XML ------------------ --------------------

<RadioGroup
    android:id="@+id/group"
    android:layout_marginTop="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <RadioButton
        android:id="@+id/rdAdministracao"            
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Administração"/>

    <RadioButton
        android:id="@+id/rdContabeis"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ciências Contábeis"/>

    <RadioButton
        android:id="@+id/rdDireito"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Direito"/>

    <RadioButton
        android:id="@+id/rdEngCivil"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Engenharia Civíl"/>

    <RadioButton
        android:id="@+id/rdEngProd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Engenharia de Produção"/>

    <RadioButton
        android:id="@+id/rdEngEletrica"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Engenharia Elétrica"/>

    <RadioButton
        android:id="@+id/rdEngQuimica"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Engenharia Química"/>

    <RadioButton
        android:id="@+id/rdEngZootecnia"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Zootecnia"/>
</RadioGroup>
    
asked by anonymous 27.01.2018 / 14:20

1 answer

2

The code is correct, however, you are capturing the value of RadioGroup before the user selects an option and so the result is coming as -1 .

Correct is to capture the value within the onClick event. This way, every time the user clicks the button, the value of idSelecionado will always be updated.

@Override
public void onClick(View view) {  
    idSelecionado = String.valueOf( radioGroup.getCheckedRadioButtonId());
    Toast.makeText(getApplicationContext(),idSelecionado,Toast.LENGTH_LONG).show();
}
    
27.01.2018 / 14:59