How to set checkbox checked and disabled

12,906

You can check the checkbox by using the setChecked() method which boolean value as parameter.

Example:

   option1.setChecked(true);

and also uncheck it using

   option2.setChecked(false);

If you want to set it to checked and disabled you have use setEnabled() which takes boolean as it's parameters.

Example.

option3.setChecked(true);
option3.setEnabled(false); 

This will disable your checkbox and even check it. I hope this was helpful. ThankYou.

Share:
12,906
Abduhafiz
Author by

Abduhafiz

laravel, mysql, swift

Updated on June 28, 2022

Comments

  • Abduhafiz
    Abduhafiz almost 2 years

    I have this code for showing list of languages for download:

    public void onCreateDialog(ArrayList<String>fullLangArray, final ArrayList<String>codeLangArray) {
    
        final String[] items = fullLangArray.toArray(new String[fullLangArray.size()]);
    
        final ArrayList mSelectedItems = new ArrayList();
    
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    
        // Set the dialog title
        builder.setTitle("Updates...")
                .setMultiChoiceItems(items, null,
                        new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int indexSelected,
                                                boolean isChecked) {
                                if (isChecked) {
                                      mSelectedItems.add(Utils.SERVER_ADDRESS + "/" + codeLangArray.get(indexSelected) + ".zip");
                                } else if (mSelectedItems.contains(indexSelected)) {
                                      mSelectedItems.remove(Integer.valueOf(indexSelected));
                                }
                            }
                        })
    
                .setPositiveButton("Download", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
    
                        DownloadTask downloadTask = new DownloadTask(MainActivity.this);
                        downloadTask.execute(mSelectedItems.toString());
    
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
    
                    }
                });
    
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }
    

    I want to make a one item of a checkbox is checked and "disabled" like in a photo (Option 3) when AlertDialog is loaded.

    enter image description here

    Can you help me how to do it?