Shahid Sarwar
Shahid Sarwar

Reputation: 1209

android: RadioButton should not get clicked if certain condition is not met

I have a radio button group in which Ive got 4 RadioButtons. I do certain operations after each RadioButton is checked now if some condition is not met I want to uncheck the RadioButton for which the condition did not meet and check the previously selected RadioButton.

For. example in the code below I am checking if internet connection is available if it is available i am calling a method... but if it is not available i am printing a toast.. but I also want to uncheck the radio button in the else part of the code.. how do i do that? Please help.

@Override
public void onCheckedChanged(RadioGroup group, int i) {
    Log.e("Position 649 :", i + grades_Array.get(i) + "");
    Boolean check_net = Utils.isConnected(context);
    if(check_net) {
        if(rdb_id!=-1)
        change_grade = grades_Array.get(i);
        change_grade = change_grade.replace(' ', '-');
        grade = change_grade;
        ProductDetails.bundle.putString("product_grade", grade);
        ProductDetails.bundle.putString("product_caret", caret);
        ProductDetails.bundle.putString("product_color", color);
        ProductDetails.bundle.putString("product_size", size_item);
        ProductDetails.bundle.putString("diamond_color", diamond_color);
        ProductDetails.bundle.putString("product_rate", rate_per_gm);
        ProductDetails.bundle.putString("product_weight", breakup_weight );
        ProductDetails.bundle.putString("product_weight", breakup_weight );
        ProductDetails.bundle.putString("product_purity", purity );
        calculation_data(productId, caret, diamond_color, grade, color, String.valueOf(check_size_Array.indexOf(size_item)), purity);
    }
    else{
        Utils.setToastCenter(context, context.getString(R.string.net_msg));
    }
}

Upvotes: 1

Views: 251

Answers (2)

Shahid Sarwar
Shahid Sarwar

Reputation: 1209

This is how I solved my issue hope it helps someone:

//I took an integer
int rdb_id=-1;
//and onCreate I did:
    rdb_id=diamond_rg.getCheckedRadioButtonId();
//AND..

    @Override
        public void onCheckedChanged(RadioGroup group, int i) {

            Boolean check_net = Utils.isConnected(context);
            if(check_net) {
                change_grade = grades_Array.get(i);
                change_grade = change_grade.replace(' ', '-');
                grade = change_grade;
                ProductDetails.bundle.putString("product_grade", grade);
                ProductDetails.bundle.putString("product_caret", caret);
                ProductDetails.bundle.putString("product_color", color);
                ProductDetails.bundle.putString("product_size", size_item);
                ProductDetails.bundle.putString("diamond_color", diamond_color);
                ProductDetails.bundle.putString("product_rate", rate_per_gm);
                ProductDetails.bundle.putString("product_weight", breakup_weight );
                ProductDetails.bundle.putString("product_weight", breakup_weight );
                ProductDetails.bundle.putString("product_purity", purity );
                whatSize=fromRadio;
                rdb_id=i;
                calculation_data(productId, caret, diamond_color, grade, color, String.valueOf(check_size_Array.indexOf(size_item)), purity);
            }
            else{
                setToastCenter(context, context.getString(R.string.net_msg));
                diamond_rg.check(rdb_id); //setting previously selected radiobutton
            }
        }

Upvotes: 0

ItamarG3
ItamarG3

Reputation: 4122

to uncheck a radio button, use this:

...
else{
    Utils.setToastCenter(context, context.getString(R.string.net_msg));
    radiobutton.setOnCheckedChangeListener (null);        
    radiobutton.setChecked(false);
    radiobutton.setOnCheckChangeListener(mListener);
}

where radiobutton is the radio button you want to uncheck. You have to save the radio button's check listener to a temporary variable mListener

Upvotes: 3

Related Questions