javaandroidandroid-checkbox

How to use setOnCheckedChangeListener for multiple checkbox [Dynamic View]


i'll create a one dynamic view. in this view i can design one dynamic view with 5 CheckBox

i'll try below code for dynamic view :

private void chkdynamic() {
        final String name[] = {"Credit Card", "Debit Card","Bank Deposit","in-app Purchase", "PayPal"};
        /*int Array_Count=0;
        String[] Str_Array = new String[0];

        Array_Count = Str_Array.length;*/

        for (int i = 0; i < name.length; i++)
        {

            final View v = new View(this);
            v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, 5));
            v.setBackgroundColor(getResources().getColor(R.color.border_gray));

            LinearLayout row = new LinearLayout(this);
            row.setOrientation(LinearLayout.VERTICAL);
            row.setId(i);
            row.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
            final CheckBox checkBox = new CheckBox(activity);
            checkBox.setId(i);
            checkBox.setText(name[i]);
            checkBox.setTextColor(getResources().getColor(R.color.gray_text));
            checkBox.setButtonDrawable(getResources().getDrawable(R.drawable.chk_btn));
            checkBox.setPadding(20,60,0,60);
            checkBox.setTextSize(20);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT
            );
            params.setMargins(60, 0, 0, 0);
            checkBox.setLayoutParams(params);
            row.addView(checkBox);
            row.addView(v);
            ll_dynamic.addView(row);
        }
    }

as per the above code 5 checkbox created as per the name length.

My question is Now How to i setOnCheckedChangeListener for checkbox

if i check CheckBox one then other CheckBox Uncheck how to this possible in dynamic view.

please suggest me any solution.


Solution

  • public class MainActivity extends AppCompatActivity {
    
    LinearLayout ll_dynamic;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ll_dynamic=(LinearLayout)findViewById(R.id.hhh);
        final String name[] = {"Credit Card", "Debit Card","Bank Deposit","in-app Purchase", "PayPal"};
        /*int Array_Count=0;
        String[] Str_Array = new String[0];
    
        Array_Count = Str_Array.length;*/
    
        for (int i = 0; i < name.length; i++)
        {
    
            final View v = new View(this);
            v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, 5));
    
            LinearLayout row = new LinearLayout(this);
            row.setOrientation(LinearLayout.VERTICAL);
            row.setId(i);
            row.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
            final CheckBox checkBox = new CheckBox(this);
            checkBox.setId(1000+i);
            checkBox.setText(name[i]);
            checkBox.setPadding(20,60,0,60);
            checkBox.setTextSize(20);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT
            );
            params.setMargins(60, 0, 0, 0);
            checkBox.setLayoutParams(params);
            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked) {
                        for (int j = 0; j < name.length; j++) {
                            if (buttonView.getId() != 1000+j) {
                                CheckBox chk = (CheckBox)ll_dynamic.findViewById(1000+j);
                                chk.setChecked(false);
                            }
                        }
                    }
                }
            });
            row.addView(checkBox);
    
            row.addView(v);
            ll_dynamic.addView(row);
        }
    }
    

    }