Brian Brian - 1 month ago 8
Android Question

How do I add an EditText to my AlertDialog that also uses setMultiChoiceItems?

I am looking to create an Android AlertDialog that has a multiple-choice list of CheckBoxes as well as an EditText for an "other" option. I have been working off the example here. Currently, I have an AlertBox working just like in the example on the example page, that uses an array of options I have stored in

/res/values/array.xml
. The CheckBoxes all work, and pass information back to my activity.

What I have found out from my googleing, is that in order to use an EditText with my AlertDialog, I will need to use a custom layout. I'm all right with this, but what I cannot figure out is how can I use a custom layout and my list of items at the same time. I would, obviously, like to keep my list options in a resource file so that I do not have to hard-code each option into a CheckBox within my layout every time I want to make a change to the list of options.

So, can this be done the way I would like; using my list in
/res/values/array.xml
to populate the list of CheckBoxes and a custom layout for the "other" option? If this is possible, I need a push in the correct direction.

Thanks

edit

My class essentially looks like this:

public class MyClass extends DialogFragment{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
ArrayList mSelectedItems = new ArrayList();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.pick_toppings)
builder.setMultiChoiceItems(R.array.my_array, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
mSelectedItems.add(which);
} else if (mSelectedItems.contains(which)) {
mSelectedItems.remove(Integer.valueOf(which));
}
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// do stuff here ...
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// do stuff here ...
}
});

return builder.create();
}
}


And the
/res/values/array.xml
essentially looks like this:

<resources>
<array name="my_array">
<item>item 01</item>
<item>item 02</item>
<item>item 03</item>
<item>item 04</item>
<item>item 05</item>
</array>
</resources>


Lastly, I create my AlertDialog from my activity like this:

MyClass myClass = new MyClass();
myClass.show(getSupportFragmentManager(), "My Dialog");

Answer

Ok, I finally figured this out on my own. Here is my resolution:

New Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="right">
</LinearLayout>

New Class:

public class MyClass extends DialogFragment{
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        String[] theOptions = getResources().getStringArray(R.array.options);

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.pick_toppings)

        LayoutInflater layoutInflater = getActivity().getLayoutInflater();
        LinearLayout view = (LinearLayout) layoutInflater.inflate(R.layout.my_layout, null);

        for(String option : theOptions){
            CheckBox checkbox = new CheckBox(getContext());
            checkbox.setText(option);
            view.addView(checkbox);
        }

        LinearLayout otherLinearLayout = new LinearLayout(getContext());
        otherLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
        otherLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        otherLinearLayout.setId(R.id.otherLinearLayout);

        CheckBox otherCheckBox = new CheckBox(getContext());
        otherCheckBox.setText("other");
        otherCheckBox.setId(R.id.otherCheckBox);

        EditText otherEditText = new EditText(getContext());
        otherEditText.setId(R.id.otherEditText);

        otherLinearLayout.addView(otherCheckBox);
        otherLinearLayout.addView(otherEditText);

        view.addView(otherLinearLayout);

        builder.setView(view);

        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                // do stuff here ...
            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                // do stuff here ...
            }
        });

        return builder.create();
    }
}