Articles → Android → Multiple Choice Alert Dialog In Android

Multiple Choice Alert Dialog In Android






Example




  1. Create a button
  2. Picture showing the button in the component tree
    Click to Enlarge

  3. Create an array in resources
  4. Picture showing an array in the resources file
    Click to Enlarge

  5. Write code for button onclick event
  6. public void ButtonClicked(View view) {
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      final String[] availableTypes = getResources().getStringArray(R.array.sample_array);
      builder.setMultiChoiceItems(R.array.sample_array, null, new DialogInterface.OnMultiChoiceClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int item, boolean isChecked) {
          if (isChecked) {
            Toast.makeText(EditTextDemo.this, availableTypes[item], Toast.LENGTH_LONG).show();
          }
        }
      });
      builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
          dialog.dismiss();
        }
      });
      builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
          Toast.makeText(EditTextDemo.this, "Ok Button Clicked", Toast.LENGTH_LONG).show();
        }
      });
      builder.show();
    }



Output




Picture showing the output of the Multiple choice alert dialog in android
Click to Enlarge



Picture showing the output of the Multiple choice alert dialog in android
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Sunday, November 24, 2019

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250