Error picking up Radiogroup Android Id

Asked

Viewed 178 times

0

I’m having trouble 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>
  • You are defining the id Radiogroup in xml? If possible post part of your xml.

  • Postei....................

  • 1

    Add this code idSelecionado = String.valueOf( radioGroup.getCheckedRadioButtonId()); within the event onClick

  • It worked out! Thanks

1 answer

2


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

The correct is to capture the value within the event onClick. This way, every time the user clicks on the button, the value of idSelecionado will always be up to date.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.