XML
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/btnOrange"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="Orange"
android:fontFamily="@font/poppins_medium"
android:textColor="@color/orange" />
<RadioButton
android:id="@+id/btnBlue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="Blue"
android:fontFamily="@font/poppins_medium"
android:textColor="@color/primary" />
<RadioButton
android:id="@+id/btnGreen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="Green"
android:fontFamily="@font/poppins_medium"
android:textColor="@color/green" />
<RadioButton
android:id="@+id/btnRed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="Red"
android:fontFamily="@font/poppins_medium"
android:textColor="@color/red" />
</RadioGroup>
JAVA
RadioGroup radioGroup;
RadioButton btnOrange, btnBlue, btnGreen, btnRed;
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.btnOrange:
constraintLayout.setBackgroundColor(ContextCompat.getColor(RadioButtonActivity.this,R.color.orange));
break;
case R.id.btnBlue:
constraintLayout.setBackgroundColor(ContextCompat.getColor(RadioButtonActivity.this,R.color.primary));
break;
case R.id.btnGreen:
constraintLayout.setBackgroundColor(ContextCompat.getColor(RadioButtonActivity.this,R.color.green));
break;
case R.id.btnRed:
constraintLayout.setBackgroundColor(ContextCompat.getColor(RadioButtonActivity.this,R.color.red));
}
}
});
OUTPUT
Post a Comment