0

THE CODE

For the checkout in my app, I let the user decide the payment method he wants to use by selecting it in a RadioButton Group:

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

            <RadioButton
                android:id="@+id/radio_prepaid"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="prepaid"
                android:tag="prepaid"
                />

            <RadioButton
                android:id="@+id/radio_creditcard"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="creditcard"
                android:textAllCaps="true"
                android:tag="creditcard"
                />
        </RadioGroup>

From the user selection I want to show or hide the matching Layout:

    <RelativeLayout
    android:id="@+id/relativeLayout_card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="gone"
    >
    ...
    </RelativeLayout>

or

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/cL_preapid"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="gone"
    >
...
</ConstraintLayout>

I did it with this part of code:

    CompoundButton.OnCheckedChangeListener onPaymentChangedListener = (compoundButton,b) -> {

      if(compoundButton.getTag().equals("prepaid")) {
          relativeLayout_card.setVisibility(View.GONE);
          cl_prepaid.setVisibility(View.VISIBLE);

      }
      if(compoundButton.getTag().equals("creditcard")) {
          cl_prepaid.setVisibility(View.GONE);
          relativeLayout_card.setVisibility(View.VISIBLE);
      }

    };

    rb_creditcard.setOnCheckedChangeListener(onPaymentChangedListener);
    rb_prepaid.setOnCheckedChangeListener(onPaymentChangedListener);

THE PROBLEM

1. CLICK on my Prepaid / CreditCard RadioButton it shows the matching view perfectly

2. CLICK on my Prepaid / CreditCard RadioButton it does simply nothing

3. CLICK on my Prepaid / CreditCard RadioButton it reverses the views: "prepaid" shows the CreditCard Layout, "creditcard" shows me PrepaidPay Layout

Is this a problem with the Layouts (should I implement a ViewSwwichter / ViewPager) or is there an error in my code?

Thank you

KayD
  • 372
  • 5
  • 17

2 Answers2

1

Instead of each RadioButton try to set listener on RadioGroup

payment_method.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
    {
        public void onCheckedChanged(RadioGroup group, int checkedId)
        {
            RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedId);


            if(checkedRadioButton.isChecked()) {

                if (checkedId == R.id.radio_prepaid)
                {
                    relativeLayout_card.setVisibility(View.GONE);  
                    cl_prepaid.setVisibility(View.VISIBLE); 

                } 
                else if (checkedId == R.id.radio_creditcard)
                {
                    cl_prepaid.setVisibility(View.GONE);  
                    relativeLayout_card.setVisibility(View.VISIBLE); 

                } 
            }
        }
    });
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
0

Try to implement one listener for each button. Its easier to read and reduces code. No if branch.

You can write a lambda in each setOnCheckedChangeListener((compoundButton,b) -> {}) or better on the radiogroup.

The visibility issue isnt really clear due missing view hierarchy.

nilsjr
  • 1
  • 1