Sabyasachi
Sabyasachi

Reputation: 3599

How to validate multiple checkboxes

I have three checkboxes in one row. For every checkbox I have one layout below to that which is invisible.

1) If first check box is checked, respective layout will be visible.
2) If second checkbox is checked, respective layout will be visible.
3) If third checkbox is checked, respective layout will be visible.
4) If all checkbox are checked, all layouts will be visible.

Upvotes: 1

Views: 2663

Answers (3)

Arun Shankar
Arun Shankar

Reputation: 622

Try this code

public class MySampleActivity extends Activity {
    CheckBox cb1, cb2, cb3, cb4;
    LinearLayout l1, l2, l3, l4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        cb1 = (CheckBox) findViewById(R.id.cb1);
        cb2 = (CheckBox) findViewById(R.id.cb2);
        cb3 = (CheckBox) findViewById(R.id.cb3);
        cb4 = (CheckBox) findViewById(R.id.cb4);
        l1 = (LinearLayout) findViewById(R.id.l1);
        l2 = (LinearLayout) findViewById(R.id.l2);
        l3 = (LinearLayout) findViewById(R.id.l3);
        l4 = (LinearLayout) findViewById(R.id.l4);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        cb1.setOnCheckedChangeListener(new MyCheckedChangeListener(1));
        cb1.setOnCheckedChangeListener(new MyCheckedChangeListener(2));
        cb1.setOnCheckedChangeListener(new MyCheckedChangeListener(3));
        cb1.setOnCheckedChangeListener(new MyCheckedChangeListener(4));
    }

    public class MyCheckedChangeListener implements CompoundButton.OnCheckedChangeListener {
        int position;

        public MyCheckedChangeListener(int position) {
            this.position = position;
        }

        private void changeVisibility(LinearLayout l1, boolean isChecked) {
            if (isChecked) {
                l1.setVisibility(View.VISIBLE);
            } else {
                l1.setVisibility(View.GONE);
            }

        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            switch (position) {
                case 1:
                    changeVisibility(l1, isChecked);
                    break;
                case 2:
                    changeVisibility(l2, isChecked);
                    break;
                case 3:
                    changeVisibility(l3, isChecked);
                    break;
                case 4:
                    changeVisibility(l4, isChecked);
                    break;
            }
        }
    }
}

Upvotes: 1

Elyakim Levi
Elyakim Levi

Reputation: 370

In your xml layout file, give each checkbox an id (checkbox_1, checkbox_2, checkbox_3):

android:id="@+id/checkbox_1"

Same with layout elements (layout_1, layout_2, layout_3):

android:id="@+id/layout_1"

In your code, point to the elements:

CheckBox checkbox1 = (CheckBox) findViewById(R.id.checkbox_1);
RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.layout_1);

Add a check listener to each checkbox:

checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        if (isChecked)
        {
            layout1.setVisibility(View.VISIBLE);
        }
        else
        {
            layout1.setVisibility(View.INVISIBLE);
        }
    }
});

Same for the other two.

Upvotes: 0

SANAT
SANAT

Reputation: 9257

Check your checkbox is checked or not and show hide based on it like this :

CheckBox cb1 = (CheckBox) findViewById(R.id.cb1);
cb1.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        if (isChecked)
        {
            yourLayout.setVisibility(View.Visible);
        }
        else
        { 
            yourLayout.setVisibility(View.Gone );
        }
    }
});

Upvotes: 0

Related Questions