Can't display button value in radio group
I have a simple code below and I don't understand why it doesn't work for me. What I just want to do is display (toast) "masculine" or "feminine" depending on which switch I pressed.
Here is the code from the main activity:
private static RadioGroup radio_g;
private static RadioButton radio_b;
private static Button button_sbm;
public void onClickListenerButton() {
radio_g = (RadioGroup)findViewById(R.id.genderButton);
button_sbm = (Button)findViewById(R.id.getBMI);
button_sbm.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
int selected_id = radio_g.getCheckedRadioButtonId();
radio_b = (RadioButton)findViewById(selected_id);
Toast.makeText(MainActivity.this,
radio_b.getText().toString(),Toast.LENGTH_SHORT ).show();
}
}
);
}
And the radio group:
<RadioGroup
android:id="@+id/genderButton"
android:layout_width="124dp"
android:layout_height="67dp"
android:layout_marginLeft="89dp"
android:layout_marginStart="88dp"
android:layout_marginTop="63dp"
android:visibility="visible"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ageField"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1">
<RadioButton
android:id="@+id/maleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:text="Male"
tools:text="male" />
<RadioButton
android:id="@+id/femaleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Female"
tools:text="female" />
</RadioGroup>
Can anyone please point out what I am missing? This is frustrating.
source to share
Your code should look like this:
public class FormActivity extends Activity implements View.OnClickListener {
private static RadioGroup radio_g;
private static RadioButton radio_b;
private static Button button_sbm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form);
// initialize component here like this
radio_g = (RadioGroup)findViewById(R.id.genderButton);
button_sbm = (Button)findViewById(R.id.getBMI);
// Then call listener on button click like this
button_sbm .setOnClickListener(this);
}
@Override
public void onClick(View v) {
int selected_id = radio_g.getCheckedRadioButtonId();
radio_b = (RadioButton)findViewById(selected_id);
Toast.makeText(MainActivity.this,
radio_b.getText().toString(),Toast.LENGTH_SHORT ).show();
}
Try this, it works great, I will try.
source to share
This is an example of what a complete class should look like, you need to do some activity lifecycle research to understand which methods are called at which particular point in time. Then you will understand the concept of activity and how it should be presented.
public class MainActivity extends Activity {
// global instances
private static RadioGroup radio_g;
private static RadioButton radio_b;
private static Button button_sbm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onClickListenerButton();
}
public void onClickListenerButton() {
radio_g = (RadioGroup)findViewById(R.id.genderButton);
button_sbm = (Button)findViewById(R.id.getBMI);
button_sbm.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
int selected_id = radio_g.getCheckedRadioButtonId();
radio_b = (RadioButton)findViewById(selected_id);
Toast.makeText(MainActivity.this,
radio_b.getText().toString(),Toast.LENGTH_SHORT ).show();
}
}
);
}
}
Note . I wrote this from the start, I haven't tested it.
source to share
Here is the code in the activity structure
public class radioToButton extends AppCompatActivity {
private static RadioGroup radio_g;
private static RadioButton radio_b;
private static Button button_sbm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_to_button);
radio_g = (RadioGroup) findViewById(R.id.genderButton);
button_sbm = (Button) findViewById(R.id.getBMI);
button_sbm.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
int selected_id = radio_g.getCheckedRadioButtonId();
radio_b = (RadioButton) findViewById(selected_id);
Toast.makeText(radioToButton.this,
radio_b.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
);
}
And the XML layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
android:id="@+id/genderButton"
android:layout_width="124dp"
android:layout_height="67dp"
android:layout_marginLeft="89dp"
android:layout_marginStart="88dp"
android:layout_marginTop="63dp"
android:visibility="visible"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1">
<RadioButton
android:id="@+id/maleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:text="Male"
tools:text="male" />
<RadioButton
android:id="@+id/femaleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Female"
tools:text="female" />
</RadioGroup>
<Button
android:layout_below="@id/genderButton"
android:text="AAAAAAAA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/getBMI"/>
</RelativeLayout>
source to share