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 yourxml
.– Valdeir Psr
Postei....................
– use3265498465
Add this code
idSelecionado = String.valueOf( radioGroup.getCheckedRadioButtonId());
within the eventonClick
– Valdeir Psr
It worked out! Thanks
– use3265498465