-2

I am trying to design an app which contains a list of check boxes followed by radio group (contains 4 radio buttons each).

Concept is,until we select the particular checkbox its relevant radio group should be disabled.I tried all solutions provided at

How to disable a RadioGroup until checkbox is checked

Android: How do I enable/disable a Checkbox, depending on a Radio button being selected first

Android disable radio group in xml

http://developer.android.com/reference/android/widget/RadioGroup.html but none of them are working for me..the app is crashing out..

Here is my code:

public class Service extends Activity 
{    
    private String op1=" ",op2=" ";    
    private CheckBox ck1;
    private CheckBox ck2;  
    private RadioGroup rg1;
    private RadioGroup rg2;

    protected void onCreate(Bundle savedInstanceState) 
    {    
        super.onCreate(savedInstanceState);    
        setContentView(R.layout.activity_service); 

        ck1 = (CheckBox)findViewById(R.id.checkBox1);  
        ck2 = (CheckBox)findViewById(R.id.checkBox2);

        RadioGroup rg1 = (RadioGroup) findViewById(R.id.radioGroup1);    
        RadioGroup rg2 = (RadioGroup) findViewById(R.id.radioGroup2);  

        rg1.setOnCheckedChangeListener(new OnCheckedChangeListener()    
        {    
            public void onCheckedChanged(RadioGroup group, int checkedId) 
            {    
                switch(checkedId)
                {     
                    case R.id.radio1:    
                        op1="Normal";    
                        break;    
                    case R.id.radio2:    
                        op1="Extra";    
                        break;    
                }    

            }    
        });

        rg2.setOnCheckedChangeListener(new OnCheckedChangeListener()    
        {    
            public void onCheckedChanged(RadioGroup group, int checkedId) 
            {    
                switch(checkedId)
                {    
                    case R.id.radio3:    
                        op2="Normal";    
                        break;    
                    case R.id.radio4:    
                        op2="Extra";    
                        break;    
                }    
            }    
        });    
    }

    public void onCheckboxClicked(View view) 
    {     
        ck1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() 
        {    
            public void onCheckedChanged(CompoundButton checkBox, boolean isChecked) 
            {    
                RadioGroup rg1 = (RadioGroup) findViewById(R.id.radioGroup1);    
                for(int i = 0; i < rg1.getChildCount(); i++)
                {    
                    ((RadioGroup)rg1.getChildAt(i)).setEnabled(isChecked);    
                }    
            }    
        });    
        for(int i = 0; i < rg1.getChildCount(); i++)
        {    
            ((RadioGroup)rg1.getChildAt(i)).setEnabled(false);    
        }    
        }    
        ck2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() 
        {    
            public void onCheckedChanged(CompoundButton checkBox, boolean isChecked) 
            {    
                RadioGroup rg2 = (RadioGroup) findViewById(R.id.radioGroup2);    
                for(int i = 0; i < rg2.getChildCount(); i++)
                {    
                    ((RadioGroup)rg2.getChildAt(i)).setEnabled(isChecked);   
                }    
            }    
        });    
        for(int i = 0; i < rg2.getChildCount(); i++)
        {    
            ((RadioGroup)rg2.getChildAt(i)).setEnabled(false);    
        }    
    }
}

.xml code:

    <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="3" >

    <CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="onCheckboxClicked"
    android:text="@string/Cheese"  />

    <RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"   
    android:weightSum="2"        >

    <RadioButton
    android:id="@+id/radio1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:text="@string/Normal   />

    <RadioButton
    android:id="@+id/radio2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:text="@string/Extra  />
    </RadioGroup>
    </LinearLayout>

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <CheckBox
    android:id="@+id/checkBox2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="onCheckboxClicked"
    android:text="@string/Ketchup"  />

    <RadioGroup
    android:id="@+id/radioGroup2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:weightSum="2"        >

    <RadioButton
    android:id="@+id/radio3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"  
    android:text="@string/Normal   />

    <RadioButton
      android:id="@+id/radio4"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="0.5" 
      android:text="@string/Extra    />
    </RadioGroup>
    </LinearLayout>

And to erase the radiobutton if checkbox is deselected again

     if(isChecked)  //in switch-case
        //task
     else
        radiogroup.clearCheck();
Community
  • 1
  • 1
Prabs
  • 4,923
  • 6
  • 38
  • 59

1 Answers1

2

First of all, never initialize your class level GUI components where you declare them. These elements are context-dependent, so they need a context in which they will be created. Until then, your call to findViewById will return null. So put your initialization into the onCreate method.

Second problem: you need your rg1 and rg2 members to be class-members as well, because you refer them from outside the onCreate method too.

Third problem: you had an early closing bracket after the onCreate method which closed the class definition.

What may work for your is:

public class Service extends Activity 
{    
    private String op1=" ",op2=" ";    
    private CheckBox ck1;   
    private CheckBox ck2;    
    private RadioGroup rg1;
    private RadioGroup rg2;

    protected void onCreate(Bundle savedInstanceState) 
    {    
        super.onCreate(savedInstanceState);    
        setContentView(R.layout.activity_service);

        ck1 = (CheckBox)findViewById(R.id.checkBox1);
        ck2 = (CheckBox)findViewById(R.id.checkBox2);

        rg1 = (RadioGroup) findViewById(R.id.radioGroup1);    
        rg2 = (RadioGroup) findViewById(R.id.radioGroup2);

        rg1.setOnCheckedChangeListener(new OnCheckedChangeListener()    
        {    
            public void onCheckedChanged(RadioGroup group, int checkedId) 
            {    
                switch(checkedId)
                {     
                    case R.id.radio1:    
                        op1="Yes";    
                        break;    
                    case R.id.radio2:    
                        op1="No";    
                        break;    
                }    

            }    
        });

        rg2.setOnCheckedChangeListener(new OnCheckedChangeListener()    
        {    
            public void onCheckedChanged(RadioGroup group, int checkedId) 
            {    
                switch(checkedId)
                {    
                    case R.id.radio3:    
                        op1="Yes";    
                        break;    
                    case R.id.radio4:    
                        op1="No";    
                        break;    
                }    
            }    
        });    
    }

    public void onCheckboxClicked(View view) 
    {     
        ck1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() 
        {    
            public void onCheckedChanged(CompoundButton checkBox, boolean isChecked) 
            {    
                RadioGroup rg1 = (RadioGroup) findViewById(R.id.radioGroup4);    
                for(int i = 0; i < rg1.getChildCount(); i++)
                {    
                    ((RadioGroup)rg1.getChildAt(i)).setEnabled(isChecked);    
                }    
            }    
        });    
        for(int i = 0; i < rg1.getChildCount(); i++)
        {    
            ((RadioGroup)rg1.getChildAt(i)).setEnabled(false);    
        }    
        ck2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() 
        {    
            public void onCheckedChanged(CompoundButton checkBox, boolean isChecked) 
            {    
                RadioGroup rg2 = (RadioGroup) findViewById(R.id.radioGroup5);    
                for(int i = 0; i < rg2.getChildCount(); i++)
                {    
                    ((RadioGroup)rg2.getChildAt(i)).setEnabled(isChecked);   
                }    
            }    
        });    
        for(int i = 0; i < rg2.getChildCount(); i++)
        {    
            ((RadioGroup)rg2.getChildAt(i)).setEnabled(false);    
        }    
    }
}

Also note, that onCheckboxClicked is probably designed to be referred directly from the layout, so make sure you have it bound properly.

UPDATE based on new information:

To accomplish your task, I'd suggest you use a simpler approach, using dedicated eventlisteners:

Service.java

public class Service extends Activity implements 
    android.widget.CompoundButton.OnCheckedChangeListener, 
    android.widget.RadioGroup.OnCheckedChangeListener
{
    private String op1 = " ", op2 = " ";
    private CheckBox ck1;
    private CheckBox ck2;
    private RadioGroup rg1;
    private RadioGroup rg2;

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service);

        ck1 = (CheckBox) findViewById(R.id.checkBox1);
        ck2 = (CheckBox) findViewById(R.id.checkBox2);

        // use the same listener for each checkbox
        ck1.setOnCheckedChangeListener(this);
        ck2.setOnCheckedChangeListener(this);

        // use the same listener for each radiogroup 
        rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
        rg2 = (RadioGroup) findViewById(R.id.radioGroup2);

        rg1.setOnCheckedChangeListener(this);
        rg2.setOnCheckedChangeListener(this);
    }

    // for checkbox changes:
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        switch (buttonView.getId())
        {
            case R.id.checkBox1:
                for (int i = 0; i < rg1.getChildCount(); i++)
                {
                    rg1.getChildAt(i).setEnabled(isChecked);
                }
                break;
            case R.id.checkBox2:
                for (int i = 0; i < rg2.getChildCount(); i++)
                {
                    rg2.getChildAt(i).setEnabled(isChecked);
                }
                break;
            default:
                break;
        }
    }

    // for radiogroup changes: 
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId)
    {
        switch (checkedId)
        {
            case R.id.radio1:
                op1 = "Yes";
                break;
            case R.id.radio2:
                op1 = "No";
                break;
            case R.id.radio3:
                op2 = "Yes";
                break;
            case R.id.radio4:
                op2 = "No";
                break;
            default:
                break;
        }
    }
}

This implementation works with the fixed layout below:

activity_service.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="3" >

        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Cheese" />

        <RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="2" >

            <RadioButton
                android:id="@+id/radio1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:checked="true"
                android:enabled="false"
                android:text="@string/Normal" />

            <RadioButton
                android:id="@+id/radio2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:enabled="false"
                android:text="@string/Extra" />
        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <CheckBox
            android:id="@+id/checkBox2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Ketchup" />

        <RadioGroup
            android:id="@+id/radioGroup2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="2" >

            <RadioButton
                android:id="@+id/radio3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:checked="true"
                android:enabled="false"
                android:text="@string/Normal" />

            <RadioButton
                android:id="@+id/radio4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:enabled="false"
                android:text="@string/Extra" />
        </RadioGroup>
    </LinearLayout>

</LinearLayout>
rekaszeru
  • 19,130
  • 7
  • 59
  • 73
  • 1
    awwwsssmmmm...it's working...thank you sooo much.. :) atlast it's working...thank you..thank you ... thank you... – Prabs Dec 22 '14 at 13:21
  • @ rekaszeru if you have some time then will you please help me in this http://stackoverflow.com/questions/27602117/enable-edit-box-only-after-selecting-the-radio-button/27602476?noredirect=1#comment43627432_27602476 – Prabs Dec 22 '14 at 13:22
  • You are welcome. Please take a look at the class structure and figure out what had changed (it's commented); when you have the time, you can also improve your layout. :) – rekaszeru Dec 22 '14 at 13:22
  • You can apply the same features to your edittexts in the linked question too. – rekaszeru Dec 22 '14 at 13:24
  • ok..i'll make a try..but thank you sooooooooooooo much.. :) :) – Prabs Dec 22 '14 at 13:25
  • you may also want to accept the answer, if it solves your problem. – rekaszeru Dec 22 '14 at 13:26
  • @PrathibhaKirthi It's okay. See the answers for your other question, I've added mine too, and there are good ones pointing you to the right direction. – rekaszeru Dec 22 '14 at 14:35
  • hello.. @rekaszeru...I need some help in this code: """Button b = (Button) findViewById(R.id.button6); b.setOnClickListener(new View.OnClickListener() """ am getting error for setOnClickListener..I searched for it,then found that people were using dialog.findViewById(..) or view.findViewById(..)..but in my code i am not using any of these..so how could i refer this button id..not able to solve this since from 2 days..any suggestions would be of great help – Prabs Dec 24 '14 at 12:22
  • but how to erase the checked radiobutton when taht particular checkbox is unchecked – Prabs Jan 01 '15 at 05:02