A dialog that allows the user to select multiple entries. : Dialog « UI « Android

A dialog that allows the user to select multiple entries.

 * Copyright (C) 2010 Christian Meyer
 * This file is part of Drupal Editor.
 * Drupal Editor is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 * Drupal Editor is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with Drupal Editor. If not, see <http://www.gnu.org/licenses/>.
//package ch.dissem.android.utils;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

 * {@link BaseAdapter}-Implementation for the {@link MultiChoice} Dialog.
 * @author christian
 * @param <T>
 *            Type this Adapter contains. Should have some useful toString()
 *            implementation.
 class MultiChoiceListAdapter<T> extends BaseAdapter {
  private Context ctx;

  private Collection<T> options;
  private Collection<T> selection;
  private List<T> filteredOptions;

  public MultiChoiceListAdapter(Context context, Collection<T> options,
      Collection<T> selection) {
    this.ctx = context;

    this.options = options;
    this.selection = selection;

    this.filteredOptions = new ArrayList<T>(options.size());

  public int getCount() {
    return filteredOptions.size();

  public T getItem(int position) {
    return filteredOptions.get(position);

  public long getItemId(int position) {
    return position;

  public View getView(int position, View convertView, ViewGroup parent) {
    ChoiceView view;
    T item = getItem(position);
    boolean selected = selection.contains(item);
    if (convertView == null) {
      view = new ChoiceView(ctx, item, selected);
    } else {
      view = (ChoiceView) convertView;
      view.setItem(item, selected);
    return view;

  public void setFilter(String filter) {
    if (filter != null)
      filter = filter.toLowerCase();

    for (T item : selection)
    for (T item : options)
      if (!selection.contains(item)
          && (filter == null || item.toString().toLowerCase()

  public class ChoiceView extends CheckBox implements OnCheckedChangeListener {
    private T object;

    public ChoiceView(Context context, T object, Boolean selected) {
      this.object = object;
      setItem(object, selected);

    public void onCheckedChanged(CompoundButton buttonView,
        boolean isChecked) {
      if (selection != null) {
        if (isChecked && !selection.contains(object))
        else if (!isChecked)

    public void setItem(T object, Boolean selected) {
      this.object = object;

 * A dialog that allows the user to select multiple entries.
 * @author christian
 * @param <T>
 *            Type for this dialog. Should have some useful toString()
 *            implementation.
 class MultiChoice<T> extends Dialog {
  private ListView listView;

  private Map<T, Boolean> optionsWithSelection;
  private Collection<T> options;
  private Collection<T> selection;

  public MultiChoice(Context context, Collection<T> options,
      Collection<T> selection) {
    this.options = options;
    this.selection = selection;

  protected void onCreate(Bundle savedInstanceState) {
    Context ctx = getContext();
    LinearLayout layout = new LinearLayout(ctx);

    listView = new ListView(ctx);
    final MultiChoiceListAdapter<T> adapter;
    adapter = new MultiChoiceListAdapter<T>(ctx, options, selection);

    if (options.size() > 10) {
      EditText search = new EditText(ctx);
      search.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start,
            int before, int count) {

        public void beforeTextChanged(CharSequence s, int start,
            int count, int after) {

        public void afterTextChanged(Editable s) {



  public Map<T, Boolean> getOptionsMap() {
    return optionsWithSelection;

  public Set<T> getSelection() {
    Set<T> result = new LinkedHashSet<T>();
    for (Entry<T, Boolean> e : optionsWithSelection.entrySet())
      if (Boolean.TRUE.equals(e.getValue()))
    return result;


Related examples in the same category

1.Dialog Helper
2.Use AlertDialog to inform exception
3.Add option item to Alert dialog and get user selection result
4.Add some padding to keep the dialog borders away
5.Dialog activity and TextView
6.Preference dialog
7.Show dialog
8.Dialog layout
9.Layout dialog with xml layout file
10.extends DialogFragment
11.Color Picker Dialog
12.Dialog Yes No Message
13.Dialog Yes No Old School Message
14.Dialog Yes No Holo Light Message
15.Dialog Yes No Long Message
16.Dialog Yes No Ultra Long Message
17.Dialog with List of value
18.Dialog with Progress
19.Dialog with Single Choice
20.Dialog Multiple Choice
21.Dialog Multiple Choice Cursor
22.Dialog with Text Entry
23.Dialog with Xml layout
24.Dialog Activity
25.Demonstrates how to show an AlertDialog that is managed by a Fragment.
26.Fragment Dialog
27.Demonstrates the use of progress dialogs.
28.File open dialog
29.Show error AlertDialog
30.extends DialogPreference
31.Show Notification Alert Dialog
32.Text Dialog
33.Create Chang Log Dialog
34.Show Title And Message Dialog
35.Show dialog and parse URL
36.Error Dialog Wrapper
37.Ok Dialog Wrapper
38.Display an alert dialog
39.AlertDialog Question
40.import android.app.AlertDialog;
41.dialog Builder
42.Help Dialog Creator
43.Color Select Dialog
44.Create MessageBox