RadioButton rbOne, rbTwo, rbThree, rbFour;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rbOne = findViewById(R.id.rbOne);
rbTwo = findViewById(R.id.rbTwo);
rbThree = findViewById(R.id.rbThree);
rbFour = findViewById(R.id.rbFour);
rbOne.setOnClickListener(this);
rbTwo.setOnClickListener(this);
rbThree.setOnClickListener(this);
rbFour.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view == rbOne) {
isChecked(1);
Toast.makeText(getApplicationContext(), rbOne.getText().toString(), Toast.LENGTH_SHORT).show();
} else if (view == rbTwo) {
isChecked(2);
Toast.makeText(getApplicationContext(), rbTwo.getText().toString(), Toast.LENGTH_SHORT).show();
} else if (view == rbThree) {
isChecked(3);
Toast.makeText(getApplicationContext(), rbThree.getText().toString(), Toast.LENGTH_SHORT).show();
} else if (view == rbFour) {
isChecked(4);
Toast.makeText(getApplicationContext(), rbFour.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
private void isChecked(int position) {
switch (position) {
case 1:
rbTwo.setChecked(false);
rbThree.setChecked(false);
rbFour.setChecked(false);
break;
case 2:
rbOne.setChecked(false);
rbThree.setChecked(false);
rbFour.setChecked(false);
break;
case 3:
rbOne.setChecked(false);
rbTwo.setChecked(false);
rbFour.setChecked(false);
break;
case 4:
rbOne.setChecked(false);
rbTwo.setChecked(false);
rbThree.setChecked(false);
break;
}
}
<RadioGroup
android:id="@+id/rgMemberShip"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rbOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="One Time"
android:textSize="16sp" />
<RadioButton
android:id="@+id/rbTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="80dp"
android:text="One Time"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rbThree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="One Time"
android:textSize="16sp" />
<RadioButton
android:id="@+id/rbFour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="80dp"
android:text="One Time"
android:textSize="16sp" />
</LinearLayout>
</RadioGroup>