The Digital Ad Venture
The Digital Ad Venture

Reputation: 1606

how to add second or third EditText to an AlertDialog

Friends,

I m quite new so sorry for the basic question but after hours of searching I gave up. How do i add a second EditText to my AlertDialog? It shows just one Edittext with the two Buttons. The second EditText is not Displayed at all.

heres my code,

final AlertDialog.Builder alert = new AlertDialog.Builder(ctx);
final EditText inputstreet = new EditText(ctx);
final EditText inputstreetnumber = new EditText(ctx);

alert.setView(inputstreet);
alert.setView(inputstreetnumber);
               alert.setTitle(getResources().getString(R.string.t_MainAlertEnterAdressTitle));
// alert.setIcon(R.drawable.huji2); // Icon disabled for now
alert.setMessage(getResources().getString(R.string.t_MainAlertEnterAdressMessage));
alert.setPositiveButton(getResources().getString(R.string.t_MainAlertEnterAdressButtonOk),
        new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog,
                    int whichButton) {


                finish();
            }
        });

alert.setNegativeButton(getResources().getString(R.string.t_MainAlertEnterAdressButtonBack),
        new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog,
                    int whichButton) {

                dialog.cancel();

            }
        });
alert.show();

I removed everything whats not important. Thanks a lot!!!

Upvotes: 0

Views: 1441

Answers (1)

skynet
skynet

Reputation: 9908

Your alert dialog can only hold one view, so you have to put your EditText views inside a single layout view, like this:

final AlertDialog.Builder alert = new AlertDialog.Builder(this);

final LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);

final EditText inputstreet = new EditText(this);
final EditText inputstreetnumber = new EditText(this);

layout.addView(inputstreet);
layout.addView(inputstreetnumber);

alert.setView(layout);

Upvotes: 4

Related Questions