Android Working with RadioGroup

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="iterator.ir.a030.MainActivity">

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RadioButton
            android:text="Red"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/radioButtonRed" />

        <RadioButton
            android:text="Blue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/radioButtonBlue" />
    </RadioGroup>
</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {


    RadioGroup radioGroup;
    RelativeLayout layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        layout= (RelativeLayout) findViewById(R.id.activity_main);
        radioGroup= (RadioGroup) findViewById(R.id.radioGroup);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId)
                {
                    case R.id.radioButtonRed:
                        layout.setBackgroundColor(getResources().getColor(R.color.red));
                        break;
                    case R.id.radioButtonBlue:
                        layout.setBackgroundColor(getResources().getColor(R.color.blue));
                        break;
                }
            }
        });
    }
}

References
https://github.com/mhdr/AndroidSamples/tree/master/030