Radio button

Layout

<RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="24dp"
        android:layout_marginStart="24dp"
        android:orientation="vertical">

    <RadioButton
            android:id="@+id/sameday"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onRadioButtonClicked"
            android:text="Same day messenger service" android:layout_marginTop="16dp"/>

    <RadioButton
            android:id="@+id/nextday"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onRadioButtonClicked"
            android:text="Next day ground delivery" />

</RadioGroup>

Java

public void onRadioButtonClicked(View view) {
    boolean checked = ((RadioButton) view).isChecked();
    // Check which radio button was clicked.
    switch(view.getId()) {
        case R.id.sameday:
            if (checked)
                Toast.makeText(this, "sameday choosed", Toast.LENGTH_SHORT).show();
                break;
        case R.id.nextday:
            if (checked)
                Toast.makeText(this, "nextday choosed", Toast.LENGTH_SHORT).show();
                break;
    }
}

Last updated