Source:
public void createDialog() { final SharedPreferences p = SharedPrefsHelper.getSharedPrefs(MachineActivity.this); final Gson gson = new Gson(); MachineFilter filter; filter = gson.fromJson(p.getString("filter", null), MachineFilter.class); if(filter == null) filter = new MachineFilter(); final List<MachineFilter.State> states = new ArrayList<>(); states.addAll(filter.getStates()); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.select_options) .setMultiChoiceItems(R.array.options, filter.getStateBoolean( getResources().getStringArray(R.array.options)), new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { MachineFilter.State option = MachineFilter.State.getState( getResources().getStringArray(R.array.options)[which]); if(isChecked && !states.contains(option)) states.add(option); else states.remove(option); } }) .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { MachineFilter filter = new MachineFilter(states); SharedPreferences.Editor e = p.edit(); e.putString("filter", gson.toJson(filter)); e.apply(); appSectionsPagerAdapter.notifyDataSetChanged(); AnalyticsHelper.sendEventHit("Filters", AnalyticsHelper.CLICK, MachineFilter.State.toString(states)); } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { } }); builder.create().show(); }

Target:
public void createDialog() { final SharedPreferences p = SharedPrefsHelper.getSharedPrefs(MachineActivity.this); final Gson gson = new Gson(); MachineFilter filter; filter = gson.fromJson(p.getString("filter", null), MachineFilter.class); if(filter == null) filter = new MachineFilter(); final List<MachineFilter.State> states = new ArrayList<>(); states.addAll(filter.getStates()); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.select_options) .setMultiChoiceItems(R.array.options, filter.getStateBoolean( getResources().getStringArray(R.array.options)), new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { MachineFilter.State option = MachineFilter.State.getState( getResources().getStringArray(R.array.options)[which]); if(isChecked) { if(!states.contains(option)) states.add(option); } else states.remove(option); } }) .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { if(states.isEmpty()) { dialog.dismiss(); showTooFilteredDialog(); } else { MachineFilter filter = new MachineFilter(states); SharedPreferences.Editor e = p.edit(); e.putString("filter", gson.toJson(filter)); e.apply(); appSectionsPagerAdapter.notifyDataSetChanged(); AnalyticsHelper.sendEventHit("Filters", AnalyticsHelper.CLICK, MachineFilter.State.toString(states)); } } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { } }); builder.create().show(); }