Trying to assign each Radio Buttons in a Radio Group, with a unique Integer value or id such as 10,20,30...so-on, and return the particular value/id for further actions such as printing that id or sending it to the backend.

For eg. : for RadioButton radio_visa it should return a value of 20, for radio_mastercard, it should return 30, and so-on.

No idea how to approach. Thanks for help in advance.

My XML file:

<RadioGroup
    android:id="@+id/radioGroupCard"
    android:layout_width="151dp"
    android:layout_height="130dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.165"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.384">

    <RadioButton
        android:id="@+id/radio_visa"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onRadioButtonClickedCards"
        android:text="VISA" />

    <RadioButton
        android:id="@+id/radio_mastercard"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onRadioButtonClickedCards"
        android:text="Master Card" />

    <RadioButton
        android:id="@+id/radio_rupay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onRadioButtonClickedCards"
        android:text="Rupay" />

    <RadioButton
        android:id="@+id/radio_amex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onRadioButtonClickedCards"
        android:text="AMEX" />

</RadioGroup>

My Activity file:

public void onRadioButtonClickedCards(View view) {


    boolean checked = ((RadioButton) view).isChecked();

    
    switch (view.getId()) {
        case R.id.radio_visa:
            if (checked)
            // actions
            {
                IVCardType.setImageResource(R.drawable.visa);
            }
            break;

        case R.id.radio_mastercard:
            if (checked)
            // actions
            {
                IVCardType.setImageResource(R.drawable.mastercard);
                
            }
            break;

        case R.id.radio_rupay:
            if (checked)
            // actions
            {
                IVCardType.setImageResource(R.drawable.amex);
            }
            break;

        case R.id.radio_amex:
            if (checked)
            // actions
            {
                IVCardType.setImageResource(R.drawable.rupay);
            }
            break;

    }
}
1

There are 1 best solutions below

1
On BEST ANSWER

You can use the tag property of a RadioButton for that.

android:tag="30"

Actually, any widget have this property. Then just get it by

((RadioButton) view).getTag()